0

I have just started learning Java, so was just trying the basic input options. However the code did not work.

All I did was change the string input value before the integer input and the code started working!?

1st:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner s = new Scanner (System.in);

        int i=s.nextInt();
        double d=s.nextDouble();
        String str=s.nextLine();

        System.out.println("String :"+str);
        System.out.println("Double : "+d);
        System.out.println("int : "+i);
    }
}

2nd:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner s = new Scanner (System.in);

        String str=s.nextLine();
        int i=s.nextInt();
        double d=s.nextDouble();

        System.out.println("String :"+str);
        System.out.println("Double : "+d);
        System.out.println("int : "+i);
    }
}

Please help me understand the difference between these two codes and why the first code didn't work, but the second one did.

Miska Rantala
  • 115
  • 1
  • 2
  • 17
  • What part(s) of these *do* you understand? – Scott Hunter Jul 30 '19 at 15:55
  • @ScottHunter what i mean is when i tried the 1st code i was unable to enter string value , where as when i tried the 2nd code the program worked properly , so i did not understand how the code 2 worked but not the second one – Mohit Parekh Jul 30 '19 at 15:59
  • when you say "didn't work", what do you mean exactly? Learning to read the error messages will help you immensely when learning to code. In your first example, it expects an integer first. Did you input a number or something else? The order in which you call your scanner methods changes the type of input it's expecting to take at each step. The string can take just about anything you type, but if you type in "hello" when it expects an integer, it cannot parse that into an int. – Phaelax z Jul 30 '19 at 16:00
  • @Phaelaxz yes i took input exactly as i defined in the order , by didn't work i mean that when i first enter the integer value (ex : 1 ) and then double value (ex : 10.15) , it does not allows me to input my string , instead directly prints my print statements. – Mohit Parekh Jul 30 '19 at 16:06

1 Answers1

1

The first example does not work because nextIntand nextDouble do not consume the line terminator which is needed to move on to the next line which necessary to use nextLine as you expect. In the second scenario nextLine is performed first which does consume the line terminator so the code runs correctly.

The first example can be fixed by calling s.nextLine() an extra time and not storing the value into a variable, which will ensure the Scanner moves to the next line before performing the next operation. This is shown below:

int i=s.nextInt();
double d=s.nextDouble();
s.nextLine();
String str=s.nextLine();

This is a common people have when using nextInt() in combination with nextLine(). Make sure to move to next line correctly so you get the correct values when using them together.

Nexevis
  • 4,024
  • 3
  • 11
  • 20