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

}

}
4
  • 1
    Possible duplicate of Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods Commented Feb 9, 2017 at 14:52
  • Did this and it worked! String line = scan.nextLine(); System.out.println("Enter data separated by spaces: "); line = scan.nextLine(); Commented Feb 9, 2017 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 Commented Feb 9, 2017 at 15:00
  • Thanks for helping :) Commented Feb 9, 2017 at 15:14

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

Comments

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

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.