Fereal Fereal - 28 days ago 5x
Java Question

Java: Scanner behavior debugging

So the problem here is that when i try to run again it won't do anything.
It will run the while once then break. I know that i'm not using currentTimeMillis right does any one know what's the problem here. I'm not good at coding if you have some suggestions to improve my code feel free to tell me.
And yes I tried to look anwsers online but i didn't find anything. Sorry for bad English!

public static String list(ArrayList<String> lause2) {
String pleb = lause2.get(lause2.size() - 1);

return pleb;

public static void main(String[] args) throws Exception {
Scanner printer = new Scanner(System.in);
ArrayList<String> lause2 = new ArrayList<>();
ArrayList<Integer> keskiarvo = new ArrayList<>();

long start = System.currentTimeMillis();
long end = start + 10 * 6000;
int laskuri = 0;

boolean go = true;
boolean run = true;

System.out.println("Welcome to Typefaster");
System.out.println("Program will give you random words you will write them as fast as you can for an minute if you fail ones it's over Good luck!");

//Loads of different lause2.add("random");

while (System.currentTimeMillis() < end) {
String something = list(lause2);
System.out.println("Write " + something);
String kirjoitus = printer.nextLine();
if (kirjoitus.equals(something)) {
} else {


System.out.println("You wrote " + laskuri + " words");
int laskuri2 = 0;

System.out.println("Run again?");

char again = printer.next().charAt(0);
if (again == 'y') {
run = true;

} else if (again == 'n') {
go = false;

long start2 = System.currentTimeMillis();
long end2 = start2 + 10*6000;

while (System.currentTimeMillis() < end2 && run) {
String something1 = list(lause2);
System.out.println("Write " + something1);
String kirjoitus1 = printer.nextLine();
if (kirjoitus1.equals(something1)) {
} else {

System.out.println("You wrote " + laskuri2 + " words");




The answer is fairly easy if you know how the Scanner works. In the second run, you have this line:

String kirjoitus1 = printer.nextLine();

You assume that the method now waits for the user to do some input. Then you compare it as in the first run etc. Obviously your second loop executes the break. Why that? You can easily check this by putting System.out.println("Debug: " + kirjoitus1); right in front of it. You see, the value is an empty text, which is not equals something1, so the break gets executed.

Why is the text empty and not waiting for user input? Your last usage of the Scanner was in this line:

char again = printer.next().charAt(0);

The method next at this point did wait for user input. But it does only read the first token of the written input. The rest of the input remains in the internal buffer of the Scanner. So after this method Scanner says we have still some unread input left and it will return this unread stuff when calling nextLine() the next time.

So what you need to do is clearing the buffer after the yes/no. You can simply do that by using nextLine() instead of next() here:

char again = printer.nextLine().charAt(0);

More info at the documentation: api/.../Scanner.html