1

I am a beginner in Java programming. I am trying to write a simple program to take size of input followed by list of numbers separated by spaces to compute the sum.

The first input is getting in fine for the second one system shows error as it is trying to parse a blank string into integer. Can you please help with the mistake I am making?

import java.util.Scanner;
public class InputStringforarray {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print(" Enter size of input ");
        int num = scan.nextInt();
        System.out.println("Enter data separated by spaces: ");
        String line = scan.nextLine();
        String[] str = line.split(" ");
        int[] A = new int[num];
         int sum = 0; 
        for (int i = 0; i < num; i++)
             A[i] =Integer.parseInt(str[i]); 
        for (int i = 0; i < num; i++)
             sum = sum + A[i]; 
         System.out.println("Sum is " + sum);

}

}
Riya Kapor
  • 13
  • 3
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Arnaud Feb 09 '17 at 14:52
  • Did this and it worked! `String line = scan.nextLine(); System.out.println("Enter data separated by spaces: "); line = scan.nextLine();` – kiner_shah Feb 09 '17 at 14:56
  • 1
    while using `nextInt()` this is common problem. Solution: you have to take some input before your second input as @kiner_shah has done – jack jay Feb 09 '17 at 15:00
  • Thanks for helping :) – Riya Kapor Feb 09 '17 at 15:14

2 Answers2

1

The reason you get an exception in your code is because int num = scan.nextInt(); does not process the newline character after the number.

So when the statement String line = scan.nextLine(); is used, it processes the newline character and hence you get an empty string ""

You can either fetch the entire line and parse it to Integer, like this:

int num = Integer.parseInt(scan.nextLine());

or you can go with using nextInt() and then use a blank scan.nextLine() to process the new line after the number, like this:

int num = scan.nextInt();
scan.nextLine();
Jayesh Doolani
  • 1,103
  • 8
  • 12
0

Your Program has only one error that you were making only one scan object of scanner class, you have to make two scanner class object one will help in getting array size while another will help in getting array element.

import java.util.Scanner;
public class InputStringforarray {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
         Scanner scan1 = new Scanner(System.in); // change 1
        System.out.print(" Enter size of input ");
        int num = scan.nextInt();`enter code here`
        System.out.println("Enter data separated by spaces: ");
        String line = scan1.nextLine();// change 2
        String[] str = line.split(" ");
        int[] A = new int[num];
         int sum = 0; 
        for (int i = 0; i < num; i++)
             A[i] =Integer.parseInt(str[i]); 
        for (int i = 0; i < num; i++)
             sum = sum + A[i]; 
         System.out.println("Sum is " + sum);

}

}
Yasir
  • 1
  • 1