BLUEJ THEORY 1

The Character wrapper class has many useful methods. We will look at some of them.

boolean isDigit(char ch)

The method isDigit() accepts a char and checks if it is a digit. It will return true only if ch is a digit i.e. ch should be one of the following – ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’
Given below is an example.
1public class Test {
2 
3    public static voidmain(String[] a) {
4        boolean b1 = Character.isDigit('5');
5        boolean b2 = Character.isDigit('-');
6        System.out.println(b1);
7        System.out.println(b2);
8    }
9 
10}
Output
1true
2false

boolean isLetter(char ch)

This method returns true if ch is a letter. It can be either an uppercase letter – ‘A’, ‘B’, …. ‘Z’ or a lowercase letter.
Given below is an example program.
1public class Test {
2 
3    public static voidmain(String[] a) {
4        boolean b1 = Character.isLetter('s');
5        boolean b2 = Character.isLetter('R');
6        boolean b3 = Character.isLetter('&');
7        System.out.println(b1);
8        System.out.println(b2);
9        System.out.println(b3);
10    }
11 
12}
Output
1true
2true
3false

boolean isLetterOrDigit(char ch)

Returns true if ch is either a letter or a digit i.e. ch can be ‘A’, ‘B’, … ‘Z’, ‘a’, ‘b’, … ‘z’, ‘0’, ‘1’, … ‘9’
An example program is given below.
1public class Test {
2 
3    public static voidmain(String[] a) {
4        boolean b1 = Character.isLetterOrDigit('s');
5        boolean b2 = Character.isLetterOrDigit('9');
6        boolean b3 = Character.isLetterOrDigit('&');
7        System.out.println(b1);
8        System.out.println(b2);
9        System.out.println(b3);
10    }
11 
12}
Output
1true
2true
3false

boolean isLowerCase(char ch)

Returns true if ch is a lower case letter i.e. ch is one of ‘a’, ‘b’, … ‘z’. If ch is an uppercase letter or not a letter, then it returns false.
Example program is given below.
1public class Test {
2 
3    public static voidmain(String[] a) {
4        boolean b1 = Character.isLowerCase('s');
5        boolean b2 = Character.isLowerCase('R');
6        boolean b3 = Character.isLowerCase('&');
7        System.out.println(b1);
8        System.out.println(b2);
9        System.out.println(b3);
10    }
11 
12}
Output
1true
2false
3false

boolean isUpperCase(char ch)

This method is similar to the previous method except that it returns true if ch is an uppercase letter.
1public class Test {
2 
3    public static voidmain(String[] a) {
4        boolean b1 = Character.isUpperCase('s');
5        boolean b2 = Character.isUpperCase('R');
6        boolean b3 = Character.isUpperCase('&');
7        System.out.println(b1);
8        System.out.println(b2);
9        System.out.println(b3);
10    }
11 
12}
Output
1false
2true
3false

boolean isWhiteSpace(char ch)

A whitespace can be a space, tab or a new line. (There are few other characters which qualify as whitespace characters but we will not look at them here). If ch is a whitespace character, then this method returns true. Else, it returns false.
1public class Test {
2 
3    public static voidmain(String[] a) {
4        boolean b1 = Character.isWhitespace(' ');
5        boolean b2 = Character.isWhitespace('\t');
6        boolean b3 = Character.isWhitespace('\n');
7        boolean b4 = Character.isWhitespace('3');
8        System.out.println(b1);
9        System.out.println(b2);
10        System.out.println(b3);
11        System.out.println(b4);
12    }
13 
14}
Output
1true
2true
3true
4false

char toLowerCase(char ch)

This method takes a char and returns its lowercase equivalent. For example, if ch is ‘A’, then this method returns ‘a’. If ch is already in lowercase, then it returns the same lowercase letter without making any modifications. If ch is not a letter, it returns the same character without making any changes. No error will occur.
1public class Test {
2 
3    public static voidmain(String[] a) {
4        char c1 = Character.toLowerCase('s');
5        char c2 = Character.toLowerCase('R');
6        char c3 = Character.toLowerCase('&');
7        System.out.println(c1);
8        System.out.println(c2);
9        System.out.println(c3);
10    }
11 
12}
Output
1s
2r
3&

char toUpperCase(char ch)

This is similar to the above method except that it converts ch to uppercase.
1public class Test {
2 
3    public static voidmain(String[] a) {
4        char c1 = Character.toUpperCase('s');
5        char c2 = Character.toUpperCase('R');
6        char c3 = Character.toUpperCase('&');
7        System.out.println(c1);
8        System.out.println(c2);
9        System.out.println(c3);
10    }
11 
12}
Output
1S
2R
3&

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...