Reputation: 13
I'm currently trying to accept input into two arrays simultaneously. The reason being that the data at each position of the array is corresponding, eg. Name and ID number.
String[] arr = new String[5];
int[] arr1 = new int[5];
Scanner kb = new Scanner(System.in);
for(int i = 0; i<5; i++)
{
System.out.println("Enter a name:");
arr[i] = kb.nextLine();
System.out.println("Enter an ID:");
arr1[i] = kb.nextInt();
}
I have this code so far, but whenever i run it, it asks for a name and ID once then asks for both but only will accept ID.
I cant seem to figure out why it doesnt allow the name to be inputted, it just returns an incompatible data type error for that.
Upvotes: 1
Views: 1300
Reputation: 4808
From the second iteration your kb.nextLine()
that reads name swallows the \n
new line character that is inputted to enter the ID integer.
Actual problem is nextInt()
leaves char
tokens that are not numeric tokens, so they stay in stdin
. Whenever any other method tries to read stdin
that method takes that input. nextLine
method returns after \n
, hence the problem.
So change the code like this:
String[] arr = new String[5];
int[] arr1 = new int[5];
Scanner kb = new Scanner(System.in);
for(int i = 0; i<5; i++)
{
System.out.println("Enter a name:");
arr[i] = kb.nextLine();
System.out.println("Enter an ID:");
arr1[i] = kb.nextInt();
kb.nextLine(); //now this swallows new line
}
Or you can use two scanners: If you want there should be no relation as such giving input them...no conflicts not at all... I don't know if that is less efficient.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Tester {
public static void main(String[] args) throws Exception {
String[] arr = new String[5];
int[] arr1 = new int[5];
Scanner kb = new Scanner(System.in);
Scanner bc=new Scanner(System.in);
for(int i = 0; i<5; i++)
{
System.out.println("Enter a name:");
arr[i] = kb.nextLine();
System.out.println("Enter an ID:");
arr1[i] = bc.nextInt();
}}
}
Upvotes: 5
Reputation: 369
To me this sounds like the newline character is still stored in the input buffers after the kb.nextInt()
call, so when you call kb.nextLine()
it reads this newline character in (as an empty string, since the nextLine()
function will stop at the first newline character it encounters, hence your program asks you for the next ID.
Effectively skipping over any subsequent times asking for the name.
I'd probably add in another kb.nextLine()
call directly after the kb.nextInt()
call to clear the \n (newline) character from the buffer.
Upvotes: 0
Reputation: 72344
You're getting the behaviour you describe because nextInt()
only reads the next integer off the next line, not the remainder of the line (which is empty.) So when the loop comes back around and the nextLine()
method is called, it picks up the rest of the (empty) line. You should put a kb.nextLine()
method at the end of your loop, then it'll do as you describe.
However, inputting the data in parallel arrays like this isn't the best of practices - you should probably either create a custom class then have the array type as that class, or use a Map of IDs to names.
Upvotes: 0