I'm a beginner, just starting to take a java class!
I'm doing this for homework.
When I try to run this, it runs- but when it gets to the question that asks "Which kind- Chocolate Chip or Gingerbread Man?" and I answer, it just skips right to the next question.
Help would be MUCH appreciated- thanks! :)

import java.util.Scanner;

public class Cookies {
	public static void main(String[] args) {
		System.out.println("Why, hello there! What's your name?");
		Scanner scan = new Scanner(System.in);
		System.out.println("Hello, " + scan.next());
		System.out.println("I am the cookie master, here to help you find out if you should have a cookie.");
		System.out.println("So. Do you deserve a cookie?");
		
		String value1 = scan.next();
		
		if (value1.equalsIgnoreCase("no")) {
				System.out.println("Okay, then- no cookie for you...");
				System.out.println("...right after this one.");
				System.out.println("*COOKIE*"); }

	
		else if (value1.equalsIgnoreCase("yes")) {
			System.out.println("Are you sure about that? I mean, you HAVE been looking a little... CHUNKY lately...");

		
		String value2 = scan.next(); 
		if (value2.equalsIgnoreCase("no")) { 
			System.out.println("Okay, then- no cookie for you...");
			System.out.println("...right after this one.");
			System.out.println("*COOKIE*"); }
			
		else if (value2.equalsIgnoreCase("yes")) {
			System.out.println("Yay! Which kind- Chocolate Chip or Gingerbread Man?");

		String value3 = scan.next();
		if (value3.equalsIgnoreCase("chocolate chip")) {
			System.out.println("Hey, I like chocolate chip too!"); }
		
		else if (value3.equalsIgnoreCase("gingerbread man")) {
			System.out.println("GAH! CANNIBAL! Haha, just kidding.");}
			
			
		System.out.println("Say... what's your favourite type of cookie?");
			
		
		}
		}		
		
}
}

Recommended Answers

All 2 Replies

Scanner uses " " (space character) as a delimiter for strings by default.

So, the value3 - value will always be "chocolate" even if your user types "chocolate chip".

Solution:

Change all scan.next(); statements to scan.nextLine();

import java.util.Scanner;

public class Cookies {
	public static void main(String[] args) {
		System.out.println("Why, hello there! What's your name?");
		Scanner scan = new Scanner(System.in);
		System.out.println("Hello, " + scan.nextLine());
		System.out.println("I am the cookie master, here to help you find out if you should have a cookie.");
		System.out.println("So. Do you deserve a cookie?");
		
		String value1 = scan.nextLine();
		
		if (value1.equalsIgnoreCase("no")) {
				System.out.println("Okay, then- no cookie for you...");
				System.out.println("...right after this one.");
				System.out.println("*COOKIE*"); }

	
		else if (value1.equalsIgnoreCase("yes")) {
			System.out.println("Are you sure about that? I mean, you HAVE been looking a little... CHUNKY lately...");

		
		String value2 = scan.nextLine(); 
		if (value2.equalsIgnoreCase("no")) { 
			System.out.println("Okay, then- no cookie for you...");
			System.out.println("...right after this one.");
			System.out.println("*COOKIE*"); }
			
		else if (value2.equalsIgnoreCase("yes")) {
			System.out.println("Yay! Which kind- Chocolate Chip or Gingerbread Man?");

		String value3 = scan.nextLine();
		if (value3.equalsIgnoreCase("chocolate chip")) {
			System.out.println("Hey, I like chocolate chip too!"); }
		
		else if (value3.equalsIgnoreCase("gingerbread man")) {
			System.out.println("GAH! CANNIBAL! Haha, just kidding.");}
			
			
		System.out.println("Say... what's your favourite type of cookie?");
			
		
		}
		}		
		
}
}

It worked! Thanks a lot! :icon_cheesygrin:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.