-2

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

mhasan
  • 3,703
  • 1
  • 18
  • 37
dvr
  • 25
  • 1
  • 3

6 Answers6

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.

xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
0

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.

AKSHIT
  • 68
  • 8
0

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 );
    }

}
Ketan G
  • 507
  • 1
  • 5
  • 21
  • Ketan I am trying to print a value stored in the string for example i will take string STACK OVERFLOW as in put and I want to print the integers values stored in the string – dvr Oct 17 '16 at 15:45
0
  • 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();
        }
    
Karthikeyan KR
  • 1,134
  • 1
  • 17
  • 38
-1

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.

Zise
  • 123
  • 1
  • 1
  • 10
-1

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();
S R
  • 11
  • 6