unable to convert string to integer using parseInt

2019-09-21 13:48发布

问题:

This question already has an answer here:

  • What is a NumberFormatException and how can I fix it? [duplicate] 9 answers

As a beginner I know that Integer.parseInt() is used to convert strings to integers but here I tried a program but its not working

Public static void main(String args[])
{

    Scanner sr=new Scanner(System.in);
    String s=sr.nextLine();
    int i=Integer.parseInt(s);
    System.out.println(i);

}

I want to take a line as input and convert it into integers and print but while executing it show NumberFormatException

回答1:

Not all strings can be converted to integers.

For example, how should "xyz" be converted to an integer? There's simply no way. Java notifies the programmer of such situations with an NumberFormatExcpetion. And we programmers should handle such exception properly.

try {
    Integer.parseInt(s);
} catch (NumberFormatException e) {
    // s cannot be converted to int, do sth.
    e.printStackTrace();
}

Scanner.nextInt() will throw a different exception (InputMismatchException) for invalid inputs. Nothing is changed in terms of handling inputs that simply cannot be converted to int.



回答2:

Your code is correct and works fine.

Make sure that the number you are entering is within the limits of Integer [-2147483648,2147483647] as if the number is out of range then too it throws a NumberFormatException.

Although the preffered way to do this is to use sr.nextInt();

But what you have done also works just make sure that the number you are entering is actually int.



回答3:

Use try and catch block .If you are giving string in place of integer , it will print "Please enter a number not string.Code is given below

Public static void main(String args[])
{

    Scanner sr=new Scanner(System.in);
    String s=sr.nextLine();
    try{
    int i=Integer.parseInt(s);
    }catch(Exception e){
    System.out.println(Please enter a number not string );
    }

}


回答4:

  • You are using a line of numbers which may contain space(449 003), so it may result in exception.
  • So you can remove the white spaces before parsing it to an integer.
  • As @luke lee mentioned alphabets and special characters can not converted to integers, so use try catch blocks to handle those exceptions.

    try{
            Scanner sr=new Scanner(System.in);
            String s=sr.nextLine().replaceAll("\\s+", "");
            int i=Integer.parseInt(s);
            System.out.println(i);
        }catch (NumberFormatException e) {
            e.printStackTrace();
        }
    


回答5:

You should use sr.nextInt(). And if you are planning on looping the entier thing, you should use sr.nextLine() right after sr.nextInt(). Otherwise the enter key you press will be taken as input resulting in unpredictable outputs.



回答6:

1.Convert using Integer.parseInt()

Syntax

public static int parseInt(String s) throws NumberFormatException

The parameter s will be converted to a primitive int value. Note that the method will throw a NumberFormatException if the parameter is not a valid int.

Example

String numberAsString = "1234";
int number = Integer.parseInt(numberAsString);
System.out.println("The number is: " + number);

2.Convert using Integer.valueOf()

Example

String numberAsString = "1234";
Integer intObject = new Integer(numberAsString);
int number = intObject.intValue();

you can shorten to:

String numberAsString = "1234";
int number = new Integer(numberAsString).intValue();

or just:

int number = new Integer("1234").intValue();