Register   Login   About   Study   Enterprise   Share
AI / Internet Technology University (AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 1. Java Introduction >> 1.5. Text Processing and Collections
Current Topic: 1.5.1. Character and other Java Wrapper Classes
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Java only has a small number of primitive data types: boolean, int, float, long, double, short, byte and char.

To handle primitive data types, Java has Wrapper classes, such as Boolean, Integer, Float, Long, Double, Short, Byte and Character.
Each of the Wrapper classes has a set of methods helping to deal with the primitive data types. In processing text messages, programmers sometimes use the methods of the Character class.

Here is the list the most commonly used methods of the Character:

public static boolean islowerCase(char ch)-
Determins wether the specified character is ISO-LATIN-1 lowercase.
public static boolean isUpperCase(char ch)-
Determins wether the specified character is ISO-LATIN-1 uppercase.
public static boolean isDigit(char ch)-
Determins wether the specified character is ISO-LATIN-1 digit.
public static boolean isSpace(char ch)-
Determins wether the specified character is ISO-LATIN-1 white space.
public static char toLowerCase(char ch)-
Returns the lowercase character value of the specified ISO-LATIN-1 character. Characters that are alredy lower case are returned unmodified.
public static char toUpperCase(char ch)-
Returns the uppercase character value of the specified ISO-LATIN-1 character. Characters that are alredy uppercase are returned unmodified.
public static int digit(char ch,int radix)-
Returns numeric value of the character digit using the specified radix. If the character is not a valid digit, it returns -1.
public static char forDigit(int digit,int radix)-
Returns the character value for the specified digit in the specified radix. If the digit is not valid in the radix, the 0 character is returned.
public char charValue()-
Returns the value of this Character object.

Example


public class MyChar {
public static void main(String args[]){
char c1 = 'A';
char c2 = 'z';
char c3 = '5';

Character objectChar = new Character('O');

System.out.println("Is character "+ c1 + "uppercase? " +
Character.isUppercase(c1));
System.out.println("Is character "+ c2 + "uppercase? " +
Character.isUppercase(c2));
System.out.println("Is character "+ c3 + "a digit? " +
Character.isDigit(c3));
System.out.println("Convert "+ c1 + "to lowercase: " +
Character.toLowerCase(c1));

System.out.println("Character object objectChar is: " +
objectChar.charValue());

}
}


This program produces the following output:

Is character A uppercase? true
Is character z uppercase? false
Is character 5 a digit? true
Convert A to lowercase: a
Character object objectChar is: O
Was it clear so far?



A very often we have a need to convert a string into a number. In our previous example of finding temperature value, we might want to compare this value with a digit and send an alert, for example if the temperature today is going to be higher than 80 degrees.

We found temperature as a string: String temperature.
Before comparison with a number we will convert this string into an integer by using the method parseInt(String) of the Integer Wrapper class.

int digitalTemperature = Integer.parseInt(temperature);
if(digitalTemperature > 80) {
System.out.println("Alert: high temperature is "+digitalTemerature);
}

The Wrapper classes Float, Double, Long have similar methods: Float.parseFloat(String); Double.parseDouble(String); Long.parseLong(String);
Keep in mind that if the argument String is not a number, this conversion will cause an Exception.

It is recommended to check if an argument is numeric before the conversion.
Two methods below do exactly this, although in two different ways. The first two methods are based on the wrapper class Character, which can check every character. The third method is based on the match() method of the String class and Regular Expression. The expression provided in the match() method describes the following cases: the numbers 0-9 that might be before , + and – characters and the dot (.) character.

/**
* The isDigit() method returns true on numeric string value
* @param number
* @return true or false
*/
public static boolean isDigit(String number) {
if(number == null || number.length() == 0) return false;
for(int i=0; i < number.length(); i++) {
// each character must be a digit
if(!Character.isDigit(number.charAt(i)) )
return false;
}
return true;
}
/**
* The isDigit() method returns true on numeric string value
* @param number
* @return true or false
*/
public static boolean isDigitWithDot(String number) {
if(number == null || number.length() == 0) return false;
for(int i=0; i < number.length(); i++) {
// each character must be a digit or a dot ('.') character, so 8.6 is still numeric
if(!Character.isDigit(number.charAt(i)) && number.charAt(i) != '.')
return false;
}
return true;
}
/**
* isNumeric() method uses regular expression to check if an argument is a numeric string
* @param val
* @return true or false
*/
public static boolean isNumeric(String val) {
// each character must be a digit, dot, or +/-
return val.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+");
}

Assignments:
1. Add these methods to the Stringer class. In the main method provide several lines to test these three methods and display the results.
2. Read more about the match() method of the String class and provide another example of using Regular Expression
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>public class MyChar {
<br/>    public static void main(String args[]){
<br/>		char c1 = 'A';
<br/>		char c2 = 'z';
<br/>		char c3 = '5';
<br/>
<br/>		Character objectChar = new Character('O');
<br/>
<br/>		System.out.println("Is character "+ c1 + "uppercase? " +
<br/>		  Character.isUppercase(c1));
<br/>		System.out.println("Is character "+ c2 + "uppercase? " +
<br/>		  Character.isUppercase(c2));
<br/>		System.out.println("Is character "+ c3 + "a digit? " +
<br/>		  Character.isDigit(c3));
<br/>		System.out.println("Convert "+ c1 + "to lowercase: " +
<br/>		  Character.toLowerCase(c1));
<br/>
<br/>		System.out.println("Character object objectChar is: " +
<br/>		          objectChar.charValue());
<br/>
<br/>	}
<br/>}
<br/>
<br/>
<br/>               This program produces the following output:
<br/>
<br/>               Is character A uppercase? true
<br/>               Is character z uppercase? false
<br/>               Is character 5 a digit? true
<br/>               Convert A to lowercase: a
<br/>               Character object objectChar is: O
<br/>






Was it clear so far?




A very often we have a need to convert a string into a number. In our previous example of finding temperature value, we might want to compare this value with a digit and send an alert, for example if the temperature today is going to be higher than 80 degrees.

We found temperature as a string: String temperature.
Before comparison with a number we will convert this string into an integer by using the method parseInt(String) of the Integer Wrapper class.

int digitalTemperature = Integer.parseInt(temperature);
if(digitalTemperature > 80) {
System.out.println("Alert: high temperature is "+digitalTemerature);
}

The Wrapper classes Float, Double, Long have similar methods: Float.parseFloat(String); Double.parseDouble(String); Long.parseLong(String);
Keep in mind that if the argument String is not a number, this conversion will cause an Exception.

It is recommended to check if an argument is numeric before the conversion.
Two methods below do exactly this, although in two different ways. The first two methods are based on the wrapper class Character, which can check every character. The third method is based on the match() method of the String class and Regular Expression. The expression provided in the match() method describes the following cases: the numbers 0-9 that might be before , + and – characters and the dot (.) character.
<br/>     /**
<br/>     * The isDigit() method returns true on numeric string value
<br/>     * @param number
<br/>     * @return true or false
<br/>     */
<br/>    public static boolean isDigit(String number) {
<br/>        if(number == null || number.length() == 0) return false;
<br/>        for(int i=0; i < number.length(); i++) {
<br/>        	// each character must be a digit
<br/>            if(!Character.isDigit(number.charAt(i)) ) 
<br/>            	return false;
<br/>        }
<br/>        return true;
<br/>    }
<br/>    /**
<br/>     * The isDigit() method returns true on numeric string value
<br/>     * @param number
<br/>     * @return true or false
<br/>     */
<br/>    public static boolean isDigitWithDot(String number) {
<br/>        if(number == null || number.length() == 0) return false;
<br/>        for(int i=0; i < number.length(); i++) {
<br/>        	// each character must be a digit or a dot ('.') character, so 8.6 is still numeric
<br/>            if(!Character.isDigit(number.charAt(i)) && number.charAt(i) != '.') 
<br/>            	return false;
<br/>        }
<br/>        return true;
<br/>    }
<br/>    /**
<br/>     * isNumeric() method uses regular expression to check if an argument is a numeric string
<br/>     * @param val
<br/>     * @return true or false
<br/>     */
<br/>    public static boolean isNumeric(String val) {
<br/>    	// each character must be a digit, dot, or +/-
<br/>    	return val.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+");
<br/>    }
<br/>

Assignments:
1. Add these methods to the Stringer class. In the main method provide several lines to test these three methods and display the results.
2. Read more about the match() method of the String class and provide another example of using Regular Expression

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy