Code:
import java.util.*;
public class InputInJava {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
String name = sc.next();
String fName = sc.nextLine();
int number = sc.nextInt();
float Fnumber = sc.nextFloat();
double Dnumber = sc.nextDouble();
short Snumber = sc.nextShort();
byte Bnumber = sc.nextByte();
long Lnumber = sc.nextLong();
System.out.println(name);
System.out.println(fName);
System.out.println(number);
System.out.println(Fnumber);
System.out.println(Dnumber);
System.out.println(Snumber);
System.out.println(Bnumber);
System.out.println(Lnumber);
}
}
Input:
String String 1 1
1
i checked on overflow and a few other sites which poped up on google i tried different input such as Input2:
String String 1
1
1
1
1
1
1
Output is:
String
String 1
1
1.0
1.0
1
1
1
So why is it reading the input for nextLine upto 1 spaces only and then it reads next input?
#nextLinewill read up to the newline character (including spaces and numbers).#nextwill read up to a delimiter (which excludes whitespace from being read). Your current code necessitates that the six expected numbers come after the newline.