Hello, this is Atsugi!
What is the parseInt method of the Integer class?
I was asked, "Do you understand it inexperienced!", So make a note of it. Experienced people who can answer this quickly should turn around and go home.
There seems to be a way to convert a string in numeric format to the number itself. One of them is "use the parsexxx method".
This method uses the parsexxx method provided by each wrapper class. The xxx part is different for each wrapper class, and the following methods are prepared for each.
Byte class:
static byte parseByte(String s)
Short class:
static short parseShort(String s)
Integer class:
static int parseInt(String s)
Long class:
static long parseLong(String s)
Float class:
static float parseFloat(String s)
Double class:
static double parseDouble(String s)
For example, the parseInt method provided by the Integer class parses the string specified in the argument as an integer value and returns it as an int type value </ b>. Actually, it is described as follows.
String str = "124";
int i = Integer.parseInt(str);
If you try to convert a non-integer numeric string using the parseInt method, or if you try to convert a non-numeric string in the first place, a compile error will not occur, but at runtime.
I get the error "java.lang.NumberFormatException: For input string".
For example, in the following cases.
String str = "124.567";
int i = Integer.parseInt(str);
Also, if you try to convert a string of numbers that exceeds the range, you will not get a compile error, but at runtime
I get the error "java.lang.NumberFormatException: Value out of range".
For example, in the following cases.
String str = "198667234";
byte b = Byte.parseByte(str);
Please note that in either case, no error will occur at compile time.
class JSample5_1{
public static void main(String args[]){
String str1 = "124";
int i = Integer.parseInt(str1);
System.out.println(i); //124
}
}
that's all.
See you again.
Recommended Posts