Reputation: 303
I have this code, and everything is running as i want to except the face that it skips the first time it is supposed to wait for the next input by the user. Here is the code:
import java.util.Scanner;
public class mainThread {
public static int size;
public static int counter;
public static Scanner input = new Scanner(System.in);
public static String currentIn;
public static void main(String[] args){
System.out.println("Please type the desired amount of players: ");
size = input.nextInt();
String nameArray[] = new String[size];
for(int counter = 0; counter < size; counter++){
System.out.println("Please enter the name of player " + (counter+1));
currentIn = input.nextLine();
nameArray[counter] = currentIn;
System.out.println(nameArray[counter]);
}
}
}
Here is what the console says:
Please type the desired amount of players:
3
Please enter the name of player 1
Please enter the name of player 2
Jacob
Jacob
Please enter the name of player 3
It completely skips the first loop, and i can't seem to figure out why. Thanks for your time!
Upvotes: 1
Views: 204
Reputation: 7042
basically thing is when you give the input 3 and then press enter , the enter counted as a new line value which stay in the buffer and when you try to read a new line inside the loop
currentIn = input.nextLine();
for the first time it take that stored new line as an input . you can avoid this by adding currentIn = input.nextLine(); after size = input.nextInt();
or reading the input from a file.
Upvotes: 0
Reputation: 25705
Modified the program as so:
import java.util.Scanner;
public class mainThread {
public static int size;
public static int counter;
public static Scanner input = new Scanner(System.in);
public static String currentIn;
public static void main(String[] args){
System.out.println("Please type the desired amount of players: ");
size = Integer.parseInt(input.nextLine().trim());
String nameArray[] = new String[size];
for(int counter = 0; counter < size; counter++){
System.out.println("Please enter the name of player " + (counter+1));
currentIn = input.nextLine();
nameArray[counter] = currentIn;
System.out.println(nameArray[counter]);
}
}
}
The problem was that the \n
remained in the buffer and that is why the .nextLine()
was skipped..
Edit, the following program also has some input validation done :
import java.util.Scanner;
public class mainThread {
public static int size;
public static int counter;
public static Scanner input = new Scanner(System.in);
public static String currentIn;
public static void main(String[] args){
boolean valid = false;
System.out.println("Please type the desired amount of players: ");
while(valid == false){
try{
size = Integer.parseInt(input.nextLine().trim());
valid = true;
}catch(Exception e){
System.out.println("Error input, try again");
}
}
String nameArray[] = new String[size];
for(int counter = 0; counter < size; counter++){
System.out.println("Please enter the name of player " + (counter+1));
currentIn = input.nextLine();
nameArray[counter] = currentIn;
System.out.println(nameArray[counter]);
}
}
}
Upvotes: 1
Reputation: 21858
I'm not sure what the problem is, but if you replace the line
currentIn = input.nextLine();
With:
currentIn = input.next();
It will work.
Upvotes: 0
Reputation: 1500785
nextInt()
reads just the "3", but not the line break part. It's basically just reading up to the first token separator, but not consuming that. So if you typed in
3 Foo
and then hit return, it would take " Foo" as the name of the first player.
It sounds like you probably want to use nextLine()
when finding the number of players, too:
String playerCountText = input.readLine();
size = Integer.parseInt(playerCountText); // Or use a NumberFormat
Personally I've always found Scanner
to be a bit annoying like this - it sort of promises to make everything simple, but you really need to work out everything it's going to do...
Upvotes: 1