0

I am trying to convert a string value taken from the keyboard into an int value. I have done it like this before but now I am getting an error which states NumberFormatException.forInputString

Scanner input = new Scanner(System.in);
String choice = "";
int numberChoice;
System.out.println("Please select one of the following options");
choice = input.nextLine();
numberChoice = Integer.parseInt(choice); /*I am getting the error on this line*/

The input code is:

Data[] temperatures = new Data[7];

for(int i = 0; i < temperatures.length; i++)
    {
        System.out.println("Please enter the temperature for day " + (i+1));
        temperatures[i] = new Data(input.nextDouble());
    }
Brian White
  • 8,332
  • 2
  • 43
  • 67
destroted
  • 51
  • 1
  • 9
  • http://stackoverflow.com/questions/18711896/how-to-resolve-java-lang-numberformatexception-for-input-string-n-a – generalcrispy Oct 10 '14 at 17:29
  • You probably typed in something that wasn't a valid number. If that isn't the issue, there's something else causing a problem in code we can't see. Did the program give you an exception without giving you a chance to type anything in? Is there other input taking place before the above code? – ajb Oct 10 '14 at 17:38
  • I am typing in valid int variables. Yes there is something taking in input, I have added it to the original question – destroted Oct 10 '14 at 17:41

4 Answers4

1

you can use numberChoice = input.nextInt(); instead of choice = input.nextLine(); and then convert the string into integer

Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
  • 1
    Since nextInt uses parseInt to get its result, this will fail in exactly the same cases that the original code does. – Jon Kiparsky Oct 10 '14 at 17:39
0

Make sure you don't accidentally type in a space or a non-numeric character in your input line. I ran your code snippet and it works just fine.

Please select one of the following options
6546a  
Exception in thread "main" java.lang.NumberFormatException: For input string: "6546a"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at PrimeNumbers.main(PrimeNumbers.java:12)
Mohammad Najar
  • 2,009
  • 2
  • 21
  • 31
0

This should work fine, assuming you enter something that can be parsed as an int. Wrap the offending line in a try block and output the error and the contents of choice to see what's going wrong

for example, try this:

try {
  numberChoice = Integer.parseInt(choice);
}
catch (NumberFormatException nfe){
    System.out.println("Failed to parse: ##"+choice+"##"); // mark off the text to see whitespace
}

on my machine, this produces

/Users/jpk/code/junk:521 $ java SO
Please select one of the following options
two
Failed to parse: ##two##
Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
0

When you use a Scanner method that looks at one token, such as nextDouble() or nextInt(), the scanner will consume the characters in that token, but it will not consume the newline character at the end of the line.

This is fine if the next Scanner call is to another nextDouble(), nextInt(), etc., because then that call will skip over the newline.

However, if the next call is nextLine(), it will return "". nextLine() will return everything up to the next newline character; and since it hasn't yet consumed the newline character after the nextDouble() call, it will stop at that newline, and return an empty string, without giving you a chance to enter another line.

To solve this, you need to call an extra nextLine() to consume the newline:

for(int i = 0; i < temperatures.length; i++)
    {
        System.out.println("Please enter the temperature for day " + (i+1));
        temperatures[i] = new Data(input.nextDouble());
    }
input.nextLine();       // Add this to consume the newline
String choice = "";
int numberChoice;
System.out.println("Please select one of the following options");
choice = input.nextLine();
numberChoice = Integer.parseInt(choice); 
ajb
  • 31,309
  • 3
  • 58
  • 84