1

I have an issue reading the string,The scanner read the Integer and the double and it show's the output without reading the string.

I need your help.

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    double d=scan.nextDouble();
    String s=scan.nextLine();

    scan.close();
    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);

}

}

  • omar OUAZIZ - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 19 '20 at 13:42

3 Answers3

1

Thankful to help. This error occurs because the nextInt() and nextDouble() methods don't read the newline characters of your input.

You can easily fix this either by parsing int from nextLine(): Integer.parseInt(scan.nextLine()) or simply using the InputStreamReader class as:

//sample code for understanding

InputStreamReader in=new InputStreamReader(System.in);
BufferedReader read=new BufferedReader(in);
//taking input
System.out.print("Enter a number: ");
int val=Integer.parseInt(read.readLine());
System.out.println("Value entered: "+val);
0

Use nextLine() instead of nextInt() or nextDouble(). Check Scanner is skipping nextLine() after using next() or nextFoo()? for more information.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = Integer.parseInt(scan.nextLine());
        double d = Double.parseDouble(scan.nextLine());
        String s = scan.nextLine();

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

A sample run:

10
20
a
String: a
Double: 20.0
Int: 10
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
  • While that's common advice, this kinda eliminates the point of scanner, as you can no longer nextDouble() and friends. Isn't it simpler to just upgrade the delimiter to be newline-based instead? The `useDelimiter` invocation might look a bit odd, but from there it's clean, easy API. – rzwitserloot Mar 29 '20 at 23:59
0

The short of it? Don't call nextLine - it is confusing in how it operates.

If you want to read strings, use next() instead. If you want an entire line's worth, instead of a single word, update your scanner to work in 'line mode' instead of 'space mode':

Scanner scan = new Scanner(System.in);
scan.useDelimiter("\r?\n");

 // and now use as normal:

 int i = scan.nextInt();
 double d=scan.nextDouble();
 String s=scan.next();

That sets the scanner to scan up to newline characters, which are a little convoluted; on windows they are \r\n, but on other OSes they are just \n, hence why we specify: optional \r, then a required \n.

rzwitserloot
  • 44,252
  • 4
  • 27
  • 37