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.