Showing posts with label Character Class. Show all posts
Showing posts with label Character Class. Show all posts

Wednesday, October 24, 2018

Program to find the case of a character



In this program, user will enter any character and the program will show whether the entered character is in upper case or in lower case.

The first program is done comparing the ASCII value of the entered character. ASCII values of 'A' to 'Z' are within 65 to 90 and ASCII values of 'a' to 'z' are within 97 to 122.

import java.io.*;
class CaseChecking

{
 BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
char ch;



public void take() throws Exception
{
 System.out.print("\nEnter the character:");
ch=(char)br.read();
if (ch>=65 && ch<=90)
System.out.println("Entered Character is in Upper Case.");
else
System.out.println("Entered Character is in Lower Case.");
}

public static void main(String args[]) throws Exception
{
 CaseChecking ob=new CaseChecking();
ob.take();
}
}

The same program can be done using Character class static methods 'boolean isUpperCase()' or 'boolean isLowerCase()'.


import java.io.*;
class CaseChecking

{
 BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
char ch;

public void take() throws Exception
{
 System.out.print("\nEnter the character:");
ch=(char)br.read();
if (Character.isUpperCase(ch))
System.out.println("Entered Character is in Upper Case.");
else
System.out.println("Entered Character is in Lower Case.");
}

public static void main(String args[]) throws Exception
{
 CaseChecking ob=new CaseChecking();
ob.take();
}
}

Related Post: Programs on Character Class

Sunday, October 21, 2018

Changing Case of Character in BlueJ



import java.io.*;
class CaseChanging

{
 BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
char ch;

public void take() throws Exception
{
 System.out.print("\nEnter the character:");
ch=(char)br.read();
System.out.println("Entered Character is:”+ch);
ch=Character.toUpperCase(ch);
System.out.println("Modified Character is:”+ch);
}



public static void main(String args[]) throws Exception
{
 CaseChanging ob=new CaseChanging ();
ob.take();
}
}


Related Post: Programs on Character Class

Monday, October 15, 2018

BlueJ Program on Letter Checking in a sentence



import java.io.*;
class LetterCh
{
  BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
int I,len,c=0;
char ch;
public void showLetters () throws Exception
{
 System.out.print("\nEnter the sentence:");
String str=br.readLine();
str=str.trim();
len=str.length ();
for (i=0;i< len;i++)


{
ch= str.charAt(i);
if(Character.isLetter(ch))
c++;
}
System.out.print(“Total number of Letters in the sentence=”+c);
}

public static void main(String args[])
{
LetterCh ob=new LetterCh ();
ob. showLetters();
}
}


Related Post: Programs on Character Class

Thursday, October 11, 2018

BlueJ Program on Digit Checking in a sentence



class DigitCh
{

int I,len,c=0;
char ch;
public void showDigits (String str)
{
str=str.trim();
len=str.length ();
for (i=0;i< len;i++)


{
ch= str.charAt(i);
if(Character.isDigit(ch))
c++;
}
System.out.print(“Total number of digits in the sentence=”+c);
}

public static void main(String args[])
{
DigitCh ob=new DigitCh ();
ob. showDigits(“abcd123”);
}
}


Related Post: Programs on Character Class

Wednesday, October 10, 2018

BlueJ Program on Letter or Digit Checking in a sentence



import java.io.*;
class LetterCh
{
  BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
int I,len,c=0;
char ch;
public void showLettersDigits () throws Exception
{
 System.out.print("\nEnter the sentence:");
String str=br.readLine();
str=str.trim();
len=str.length ();
for (i=0;i< len;i++)


{
ch= str.charAt(i);
if(Character.isLetterOrDigit(ch))
c++;
}
System.out.print(“Total number of Letters and Digits in the sentence=”+c);
}

public static void main(String args[])
{
LetterCh ob=new LetterCh ();
ob. showLettersDigits();
}
}


Related Post: Programs on Character Class

Monday, October 8, 2018

Character Class in Java



Character is a class of java.lang package. Character  is a simple wrapper class and constructor of this class is Character (char ch), where the char ch is wrapped by the Character  object. Character class has several static methods which are used for several purposes


Return Type
Prototype
Use
boolean
static boolean isDigit (char ch)
Returns true if the argument character is digit otherwise false
boolean
static boolean isLetter (char ch)
Returns true if the argument character is an alphabet  otherwise false
boolean
static boolean isLetterOrDigit (char ch)
Returns true if the argument character is digit or alphabet otherwise false
boolean
static boolean isLowerCase (char ch)
Returns true if the argument character is a lowercase letter otherwise false
boolean
static boolean isUpperCase (char ch)
Returns true if the argument character is a uppercase letter otherwise false
boolean
static boolean isWhitespace (char ch)
Returns true if the argument character is a white space otherwise false
char
static char toLowerCase (char ch)
Returns lower case equivalent of the argument  character if it is in upper case otherwise returns the same value
char
static char toUpperCase (char ch)
Returns upper case equivalent of the argument  character if it is in lower case otherwise returns the same value


Related Post: Programs on Character Class

Subscribe via email

Enter your email address:

Delivered by FeedBurner