me and my noobish brother are having problems with Scanners

The problem is, every time we type “no”, we have to type it twice??
We don’t understand why? Please help… :slight_smile:

package dandd.aanda;

import java.util.Scanner;

public class TestPrintf {
	public static void main(String[] args)  {
		
		String yes = "yes";
		String no = "no";
		
		System.out.println("type yes or no");
		
		Scanner input = new Scanner(System.in);
		
		if (input.nextLine().equals(yes)) {
			System.out.println("Follow me then boooyyyoo!!!!");
		} else if (input.nextLine().equals(no)) {
			System.out.println("Alright then laddy, you just going to sit on the porch all day and crrry then?");
		} else {
			System.out.println("that didn't help me, yes or no?");
		}
		
	}

input.nextLine() requests input each time you call it.


String inputValue;
...
inputValue = input.nextLine();

if(inputValue.equals(yes)) {
...
} else if(inputValue.equals(no)) {
...
} else {
...
}

package dandd.aanda;

import java.util.Scanner;

public class TestPrintf {
	public static void main(String[] args)  {
		
		String yes = "yes";
		String no = "no";
		
		System.out.println("type yes or no");
		
		Scanner input = new Scanner(System.in);

		String line = input.nextLine();
		if (line.equals(yes)) {
			System.out.println("Follow me then boooyyyoo!!!!");
		} else if (line.equals(no)) {
			System.out.println("Alright then laddy, you just going to sit on the porch all day and crrry then?");
		} else {
			System.out.println("that didn't help me, yes or no?");
		}
		
	}

Try that ^
When you call input.nextLine(), it waits for input, so first it waits for input, then checks if it’s yes. Then it waits for input again, and checks if it’s no. to solve it, just call input.nextLine() once, and store it in a variable.

EDIT: Dammit UprightPath! I thought I was helping someone, and I get “Warning - while you were typing a new reply has been posted. You may wish to review your post.” Why am I always too slow to help people?

well everytime you do call input.nextLine() you get it and then it empties the buffer.

just do String answer = input.nextLine()

and then do if-cases on it

synchronous answer x 3 bonus !

I’ve been practicing short-ish answers so that I can be the first!

Short-ish answers, huh? I will learn this new practice, and once I have mastered it, it will the the end of your reign as first poster!

First of all, thanks to all that replied SO FAST!!! and second, me and my VERY noobish brother totally just LOL-ed ALL over the floor :smiley: it was grand! Thanks for the java-lin exp. :smiley: and IT WORKS!!! WWOOOOOTTTT!!!

C-C-C-C-C-C-COMBO BREAKER!!!