-1

I'm writing my solution for a problem which involves, among other things, parsing a string which contains numbers entered by the user, separated with spaces and then storing them as integers.

I'm not sure why I get a numberformat exception, when I'm using the nextLine() method to first accept the string (including spaces) and then using the split method to separate out the integers. What's still weird is that the code has worked in a different problem before, but not here apparently.

Here's the code, and the exception message:

package algorithms.Warmup;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by manishgiri on 4/8/15.
 */
public class ChocolateFeastTest {

    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {

        System.out.println("Enter number of test cases:");
        int T = sc.nextInt();
        ArrayList<Integer> result = new ArrayList<>(T);


        for(int i = 0; i < T; i++) {

            int[] numbers = new int[3];
            System.out.println("Enter N, C, M separated by spaces");

            String next = sc.nextLine();

            String[] nextSplit =  next.split(" ");


            int item;

            for(int p = 0; p < 3; p++) {
                item = Integer.parseInt(nextSplit[p]);
                numbers[p] = item;
            }

            int N = numbers[0];
            int C = numbers[1];
            int M = numbers[2];

            System.out.println(N + C + M);
        }

    }
}

And the exception messages:

Enter number of test cases:
2
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
Enter N, C, M separated by spaces
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at algorithms.Warmup.ChocolateFeastTest.main(ChocolateFeastTest.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Process finished with exit code 1

On tracing the exception, it looks like the error occurs in the line when I use Integer.parseInt(), but even before that, why doesn't the code read in the numbers (with spaces) in the first place? ie: this line doesn't work:

String next = sc.nextLine()

I'd appreciate any help!

Manish Giri
  • 2,949
  • 6
  • 38
  • 66
  • I'd start by checking the number of elements which `String[] nextSplit = next.split(" ");` produces when you only provide a single character – MadProgrammer Apr 08 '15 at 05:39
  • 1
    Best guess: `nextInt` isn't consuming the newline after `2`, so the `nextLine` function ends up reading the rest of the first line (which is just blank). – nneonneo Apr 08 '15 at 05:39
  • 1
    http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-next-nextint-or-other-nextfoo-methods/13102066#13102066 – Daniel Nugent Apr 08 '15 at 05:43

5 Answers5

2

You're using nextInt() which only reads the integer, not the new line character \n at the end of the line.

Therefore, when you press an integer and then enter, the line:

int T = sc.nextInt();

Only reads the integer. Next when you do:

String next = sc.nextLine();

It reads the new line character waiting in the input to be read.

Simply change to:

int T = Integer.parseInt(sc.nextLine());

(But of course, doing try/catch on that would be much better)

Ori Lentz
  • 3,600
  • 6
  • 20
  • 28
2

The problem is nextInt() doesnt use full line so when you do String next = sc.nextLine(); It reads the same line resulting the error.

Problem can be solved by

int T = sc.nextInt();
nextLine();            //adding a next line after nextInt()
singhakash
  • 7,613
  • 4
  • 25
  • 59
0

I just changed: int T = sc.nextInt(); To: int T = Integer.parseInt(sc.nextLine());

This seems to work.

Kick Buttowski
  • 6,371
  • 12
  • 34
  • 54
Phoenix
  • 315
  • 1
  • 9
0

I tried using BufferedReader and it worked. Here is the code:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.Scanner;

public class ChocolateFeastTest {

    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws IOException {

        System.out.println("Enter number of test cases:");
        int T = sc.nextInt();
        ArrayList<Integer> result = new ArrayList<>(T);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        for(int i = 0; i < T; i++) {

            int[] numbers = new int[3];
            System.out.println("Enter N, C, M separated by spaces");

            String next = br.readLine();

            String[] nextSplit = next.split(" ");

            int item;

            for(int p = 0; p < 3; p++) {
                item = Integer.parseInt(nextSplit[p]);
                numbers[p] = item;
            }

            int N = numbers[0];
            int C = numbers[1];
            int M = numbers[2];

            System.out.println(N + C + M);
        }

    }
}
Kick Buttowski
  • 6,371
  • 12
  • 34
  • 54
RishikeshD
  • 189
  • 2
  • 4
0

You need to call 'sc.nextLine()' twice, in order to take the input from the next line. The first call will take you to the next line and the second call will grab the input on the second line.

        String next = sc.nextLine();
        next = sc.nextLine();