[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~

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.

◆ Conversion from character string to numerical value

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".

◆ 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)

◆ Usage example: parsexxx method

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.

◆ Sample

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

[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~
From Java9, the constructor of the class corresponding to primitive types is deprecated.
Benefits of Java static method
[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~
Numerical value (int) ⇔ character string (String) conversion
[Java version] The story of serialization
Story of passing Java Gold SE8
[Java small story] Monitor when a value is added to the List
Story of paiza.jp [Java standard input solution]
[Basic knowledge of Java] About type conversion
The story of writing Java in Emacs
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
[Rails] How to omit the display of the character string of the link_to method
About the method to convert a character string to an integer / decimal number (cast data) in Java
[Java] How to get to the front of a specific string using the String class
[Java] Check if the character string is composed only of blanks (= Blank)
The order of Java method modifiers is fixed
[Java] How to easily get the longest character string of ArrayList using stream
Increment with the third argument of iterate method of Stream class added from Java9
[Java] Calculate the day of the week from the date (Calendar class is not used)
[Java] How to use compareTo method of Date class
[Java] How to erase a specific character from a character string
[Java] Handling of character strings (String class and StringBuilder class)
How to get the class name / method name running in Java
Get to the abbreviations from 5 examples of iterating Java lists
What is the LocalDateTime class? [Java beginner] -Date and time class-
[Java] How to convert a character string from String type to byte type
Change only one character from the String type string to uppercase
A program that searches for a character string, and when the search character string is found, displays the character string from the beginning of the line to just before the search character string.
[Java] I felt as a java beginner. "Why is the first letter of the String type capitalized?" I faced the String type.
When the character string passed to C ++ by JNA is garbled
[Java] Wrapper that executes private method of object from outside class
[Android, Java] Method to find the elapsed date from two dates
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder
How to check for the contents of a java fixed-length string
Investigation method when the CPU of the server running java is heavy
Various methods of Java String class
Numerical value (int) ⇔ character string (String) conversion
Various methods of the String class
Java, interface to start from beginner
The road from JavaScript to Java
[Java] Conversion from array to List
[Java Servlet] The road of Senri is also the fifth step from the first step
[Java Servlet] The road of Senri is also one step to the first
To you who lament that the conversion of JODConverter + LibreOffice is slow
[Java Servlet] The road of Senri is also the third step from the first step
[Java] How to use substring to cut out a part of a character string
How to find out the Java version of a compiled class file
Iterative processing of Ruby using each method (find the sum from 1 to 10)
I translated the grammar of R and Java [Updated from time to time]
[Java Servlet] The road of Senri is also the fourth step from the first step
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
[Ruby] I want to make an array from a character string with the split method. And vice versa.
I tried to make a program that searches for the target class from the process that is overloaded with Java
[Java] How to use the File class
[Java] How to use the HashMap class
[Ruby] From the basics to the inject method
[Beginner] Java method / class / external library [Note 23]
What is the main method in Java?
[Processing × Java] How to use the class
Deletes after the specified character from the character string
[Java] Comparison of String type character strings
[Java] How to use the Calendar class
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
Part of the class called from the class under test in JMockit Mock method memo
[Beginner] Points to be aware of after Java exercises / Inheritance / Abstract method [Note 26]
[Java] Where is the implementation class of annotation that exists in Bean Validation?