Reputation: 833
My entire code is posted below. My issue can be seen in this output:
Enter the character representation of your first configuration: p
How many symbols are under this configuration? 3
Enter the first symbol: Enter the first symbol: 1
Enter the first symbol: None
[, 1, None]
As you can see, the first iteration of the for loop is executed without getting user input. It appears as if it has been skipped.
My question is this: How do I create a For loop that will ask for user input for each iteration of the loop? Not skip an iteration as my code is doing now. I've looked all over the internet for the answer, but I can't find anything specifically.
import java.util.Scanner;
import java.util.ArrayList;
class read {
public static void main(String[] args) {
// Create instance of class
Tape instructions = new Tape();
// Assign scanner to keyboard variable
Scanner keyboard = new Scanner(System.in);
// Get user input for mConfig and convert to char
System.out.print("Enter the character representation of your first configuration: ");
String userMConfig = keyboard.nextLine();
char c = userMConfig.charAt(0);
// Get number of symbols/operations/f-configurations
System.out.print("How many symbols are under this configuration? ");
int numOfSymbols = keyboard.nextInt();
// Define array of symbols
ArrayList<String> userSymbols = new ArrayList<String>();
for (int i = 1; i<= numOfSymbols; i++)
{
System.out.println("Enter the first symbol: ");
String userInputSymbol = keyboard.nextLine();
userSymbols.add(userInputSymbol);
}
// Assign values to object methods
instructions.mConfig(c);
instructions.symbols(userSymbols);
// Testing code
System.out.println(instructions.getSymbols());
}
}
Here is just my For loop:
for (int i = 1; i<= numOfSymbols; i++)
{
System.out.println("Enter the first symbol: ");
String userInputSymbol = keyboard.nextLine();
userSymbols.add(userInputSymbol);
}
Upvotes: 1
Views: 50342
Reputation: 1436
The problem here is that nextInt()
reads an int, and leaves the newline character in the stream. Then, call to nextLine()
reads this character and return empty string. To avoid that use the following:
int numOfSymbols = keyboard.nextInt();
keyboard.nextLine(); // this will swallow the "\n"
// Now your for loop should work fine
for (int i = 0; i < numOfSymbols; i++)
{
System.out.println("Enter the first symbol: ");
String userInputSymbol = keyboard.nextLine();
userSymbols.add(userInputSymbol);
}
Upvotes: 4
Reputation: 34146
This happens becasue nextInt()
doesn't consume the end-of-line character.
What can you do? You can consume it manually after the nextInt()
call:
int numOfSymbols = keyboard.nextInt();
...
keyboard.nextLine(); // Consume the end-of-line character
for (int i = 1; i <= numOfSymbols; i++) {
...
}
You can refer to this post if you want more information.
Upvotes: 1
Reputation: 726479
When you do this
int numOfSymbols = keyboard.nextInt();
the scanner takes the integer portion from your input line, but leaves '\n'
in the buffer. That is why when you call
String userInputSymbol = keyboard.nextLine();
from the first iteration of your loop, that '\n'
gets returned immediately, producing an empty line.
To fix this problem, add keyboard.nextLine();
right after reading numOfSymbols
:
int numOfSymbols = keyboard.nextInt();
keyboard.nextLine(); // Throw away the '\n' from the line that contained int
Alternatively, you could use keyboard.nextLine()
followed by parsing an int
manually for reading the integer.
In general, you should be careful mixing nextLine
with other calls to Scanner
's methods, precisely because of the issue of trailing '\n'
characters.
Upvotes: 4