I have a problem with my scanner and nextLine. What happens is that it skips the first lap of the loop. My guess is that the scanner already contains something here, like a line break or something. It did work if I used two different scanners for the strCount and the one in the loop. Is this right and if it is, is there any way I can make this work without using two different scanners.
import java.util.*;
public class chars_in_string {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
System.out.print("Number of strings?");
int strCount = key.nextInt();
String [] array = new String[strCount];
for(int x = 0; x < strCount; x++){
System.out.print("String "+(x+1)+":");
array[x] = key.nextLine();
}
}
}
Example of the input/output:
Number of strings? 8
String 1:String 2:
From here it works fine to enter any string and it will just jump 1 step in the loop to get the next one.