JavaRush /Java Blog /Random EN /Q&A: how to convert String to int correctly in Java?

Q&A: how to convert String to int correctly in Java?

Published in the Random EN group
Approximate level of required knowledge: Java Syntax quest and some Java Core.
Q&A: how to convert String to int correctly in Java?  - 1
At the end of the first CodeGym quest, we learn about type conversion. In the level 10 lectures, you could see that converting intto Stringis very simple, and in general, almost any primitive type is cast to Stringwithout problems.
int x = 5;
String text = "X=" + x;
Also, those who have already studied up to level 10 of the Java Syntax quest know that it is impossible to cast a reference to a number to a reference to a string. So what's the best way to convert a string to a primitive integer? It's better to do this with a parseIntclass method Integer. The method parseIntmust convert Stringto intand throws an exception NumberFormatExceptionif the string cannot be converted to int.

Example

The main line that converts a string to an integer:
int i = Integer.parseInt (myString);
If the string denoted by variable myStringis a valid integer, such as "1", "200", Java will quietly convert it to the primitive data type int. If for some reason this fails, this action can throw an exception NumberFormatException, so to make the program work correctly for any line, we need a little more code. A program that demonstrates the conversion method Java Stringto int, controls for the possible NumberFormatException:
public class JavaStringToIntExample
{
  public static void main (String[] args)
  {
    // String s = "fred"; // use this if you need to test the //exception below
String s = "100";

    try
    {
      // this is where String is converted to int
      int i = Integer.parseInt(s.trim());

      // display the value after conversion
      System.out.println("int i = " + i);
    }
    catch (NumberFormatException nfe)
    {
      System.out.println("NumberFormatException: " + nfe.getMessage());
    }
  }

Discussion

When you study the example above, you will see that the method Integer.parseInt (s.trim ())is used to turn a string sinto an integer i, and this happens in the following line of code:
int i = Integer.parseInt (s.trim ())
But sometimes converting a string to a number just won't work. In our example, this string is the name fred. You can talk about codes in which letters are written in the computer, but, technically speaking, fred is not a number at all, and it is better to leave it as a string. Our program is organized so that when you try to convert "fred" to a number, the process Integer parseIntwill throw an exception NumberFormatException, which you must handle in a block try / catch. In this case, you don't have to use the trim ()class method String, but in real programming you should. So we have shown it to you. Since such a topic has come up, here are a few “hints” for you on the topic of classes Stringand Integer:
  • Integer.toString (int i)used to convert intto Java strings.
  • If you want to convert an object Stringto an object Integer(rather than a primitive class int), use a method valueOf ()on the class Integerinstead of a method parseInt ().
  • If you need to convert strings to additional Java primitive fields, use methods like this Long.parseLong ()one.
Answered by: Alice Watson
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION