incorrect results from if statement

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1088 Views - Last Post: 03 August 2013 - 02:16 PM Rate Topic: -----

#1 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 606
  • Joined: 10-March 13

incorrect results from if statement

Posted 01 August 2013 - 07:30 PM

This is really pissing me off. For some reason when I enter the ingredients for "caffe vanilla" I keep getting my answer wrong. I dont understand why. I've typed in the answer 6 times now and I keep getting it wrong, yet for the other two drinks I get them right. WTF. Im not getting any compiler errors either. Im guessing I messed up somewhere near line 43.

**EDIT** Nevermind, I found the problem: I missed spelled 'coffee' in the beginning string for line 8 >.<
ahhh the importance of spelling.

import java.util.Scanner;

public class Starbucks{

	public static void main(String[] args){
	
		Scanner input = new Scanner(System.in);
		String caffeVanilla2 = "milk, coffe, ice, vanilla bean powder, and whipped cream";
		String caramel2 = "milk, coffee, ice, caramel syrup, whipped cream, and caramel sauce";
		String crc2 = "caramel, milk, ice, dark caramel sauce, whipped cream, caramel drizzle, and crunchy sugar";

		//create object
		Frappuccinos frap = new Frappuccinos();
		
		//ask user what they want to study
		System.out.println("Which frappucino would you like to go over: caffe vanilla, caramel, or caramel ribbon crunch? ");
	
		String answer = input.nextLine();
		String cv = "caffe vanilla";
		String c = "caramel";
		String crc = "caramel ribbon crunch";
		
		String a1;
		String a2;
		String a3;
		String a4;
		String a5;
		String a6;
		String a7;
	
		//ask them to enter the ingredients of the drink they want to go over
		
		//match the ingredients for caffe vanilla
		if (answer.equalsIgnoreCase(cv)){
		
			System.out.println("Enter each (5) ingredient, followed by ENTER for " + answer + ":");
			a1 = input.nextLine();
			a2 = input.nextLine();
			a3 = input.nextLine();
			a4 = input.nextLine();
			a5 = input.nextLine();
						
			if (caffeVanilla2.toLowerCase().contains(a1.toLowerCase()) && caffeVanilla2.toLowerCase().contains(a2.toLowerCase()) &&  
				caffeVanilla2.toLowerCase().contains(a3.toLowerCase()) && caffeVanilla2.toLowerCase().contains(a4.toLowerCase()) && 
				caffeVanilla2.toLowerCase().contains(a5.toLowerCase())){
			
				System.out.println("Congrats you got ALL of the ingredients right!");
				frap.yesCaffeVanilla();
			}
			
			else{
				System.out.println("Im sorry. At least one of your inputs are incorrect");

			}
		}
		//match for caramel
		else if (answer.equalsIgnoreCase(c)){
		
			System.out.println("Enter each (6) ingredient, followed by ENTER for " + answer + ":");
			a1 = input.nextLine();
			a2 = input.nextLine();
			a3 = input.nextLine();
			a4 = input.nextLine();
			a5 = input.nextLine();
			a6 = input.nextLine();

			if (caramel2.toLowerCase().contains(a1.toLowerCase()) && caramel2.toLowerCase().contains(a2.toLowerCase()) && 
				caramel2.toLowerCase().contains(a3.toLowerCase()) && caramel2.toLowerCase().contains(a4.toLowerCase()) && 
				caramel2.toLowerCase().contains(a5.toLowerCase()) && caramel2.toLowerCase().contains(a6.toLowerCase())){
				
				System.out.println("Congrats you got ALL of the ingredients right!");
				frap.yesCaramel();
			}
			else{
				System.out.println("Im sorry. At least one of your inputs are incorrect");
				frap.yesCaramel();
			}
		}
		
		//match for ribbon crunch
		else if (answer.equalsIgnoreCase(crc)){
			
			System.out.println("Enter each (7) ingredient, followed by ENTER for " + answer + ":");
			a1 = input.nextLine();
			a2 = input.nextLine();
			a3 = input.nextLine();
			a4 = input.nextLine();
			a5 = input.nextLine();
			a6 = input.nextLine();
			a7 = input.nextLine();

			if (crc2.toLowerCase().contains(a1.toLowerCase()) && crc2.toLowerCase().contains(a2.toLowerCase()) &&
				crc2.toLowerCase().contains(a3.toLowerCase()) &&crc2.toLowerCase().contains(a4.toLowerCase()) &&
				crc2.toLowerCase().contains(a5.toLowerCase()) &&crc2.toLowerCase().contains(a6.toLowerCase()) &&
				crc2.toLowerCase().contains(a7.toLowerCase())){
				
				System.out.println("Congrats you got ALL of the ingredients right!");
				frap.yesCRC();
				}
			else{
				System.out.println("Im sorry. At least one of your inputs are incorrect");
				frap.yesCRC();
			}
		}
		
		else{
			System.out.println("Invalid option");
		}
		
		//ask if they would like a custom drink
		
		//search if the stuff they entered is correct
	}
}

class Frappuccinos{

	//ingredients
	String caffeVanilla2 = "milk, coffee, ice, vanilla bean powder, whipped cream";
	String caramel2 = "milk, coffee, ice, caramel syrup, whipped cream, and caramel sauce";
	String crc2 = "caramel, milk, ice, dark caramel sauce, whipped cream, caramel drizzle, and crunchy sugar";
	
	//list of fraps
	boolean caffeVanilla = false;
	boolean caramel = false;
	boolean caramelRibbonCrunch = false;
	
	
	//constructor
	Frappuccinos(){
	}
	
	//make them available with ingredients or not
	public void yesCaffeVanilla(){
		caffeVanilla = true;
		System.out.println("The caffe vanilla contains: " + caffeVanilla2);
	}
	//DO SOMETHING WITH THIS
	public void customCV(){
		caffeVanilla = true;
		System.out.println("So you want a custom caffe vanilla...you hipster! \nWhat would you like in it? Enter each item followed by pressing ENTER: ");
		//check what they enter to see if it matches shit from the string
		
	}
	//use this for a cancellation order, ask if they would like to chnage their mind
	//search entry to if else
	public void noCaffeVanilla(){
		caffeVanilla = false;
		System.out.println("Cancelling your caffe vanilla frap");
	}
	
	public void yesCaramel(){
		caramel = true;
		System.out.println("The ingredients for a caramel frap are: " + caramel2);
	}
	
	public void CustomC(){
		caramel = true;
		System.out.println("What would you like in your custom caramel frap? Enter each item followed by pressing ENTER: ");
	}
	
	public void noCaramel(){
		caramel = false;
		System.out.println("Cancelling your caraml frap...");
	}
	
	public void yesCRC(){
		caramelRibbonCrunch = true;
		System.out.println("The ingredients for a caramel ribbon crunch are: " + crc2);
	}
	
	public void customCRC(){
		caramelRibbonCrunch = true;
		System.out.println("What would you like in your caramel ribbon crunch? Enter each item followed by pressing ENTER: ");
	}
	
	public void noCRC(){
		caramelRibbonCrunch = false;
		System.out.println("Cancelling your CRC...");
	}
	
	
}




This post has been edited by streek405: 01 August 2013 - 07:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: incorrect results from if statement

#2 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2794
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: incorrect results from if statement

Posted 01 August 2013 - 07:35 PM

Is there a specific ingredient that the program flags as being wrong? You can probably step through in your debugger to find out which one specifically.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: incorrect results from if statement

Posted 03 August 2013 - 04:49 AM

cafeVanilla2 is already lowerCase no need to call lowerCase on it so many times that just make your code confusing
and never a good idea to call more than once the same method on the same object this

			a1 = input.nextLine().toLowerCase();
			a2 = input.nextLine().toLowerCase();
			a3 = input.nextLine().toLowerCase();
			a4 = input.nextLine().toLowerCase();
			a5 = input.nextLine().toLowerCase();


will simply rest of the code to

		if (caffeVanilla2.contains(a1) && 
		caffeVanilla2.contains(a2) &&
		caffeVanilla2.contains(a3) &&
		caffeVanilla2.contains(a4) &&
		caffeVanilla2.contains(a5)){


which is a lot simpler to understand and debug... and a lot less text to type :)
Was This Post Helpful? 3
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: incorrect results from if statement

Posted 03 August 2013 - 05:17 AM

Your logic is wrong. The user will get congratulated if they enter six spaces - find out why.
Was This Post Helpful? 3
  • +
  • -

#5 hax95  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 8
  • Joined: 31-July 13

Re: incorrect results from if statement

Posted 03 August 2013 - 09:15 AM

like gOOse declared, your logic is completely wrong, if you enter six spaces or six times milk for example it work.I suggest you to use a StringBuilder to join the strings:
//StringBuilder is moree efficiently in respect to a statment like String ingredients = a1+a2+a3+a4+a5;
//so i suggest you to use this 
StringBuilder ingredients = new StringBuilder();
			ingredients.append(a1);
			ingredients.append(a2);
			ingredients.append(a3);
			ingredients.append(a4);
			ingredients.append(a5);



then you call a method, for example for caffe vanilla, like this ( you write this method in your StarBucks class):

        
	public static boolean isCaffeVanilla(String ingredients){
		if(!ingredients.contains("milk")) return false;
		if(!ingredients.contains("coffee"))return false;
		if(!ingredients.contains("ice")) return false;
		if(!ingredients.contains("vanilla bean power"))return false;
		if(!ingredients.contains("whipped cream")) return false;
		return true;
	}



finally, you call this method in the main code:

if (isCaffeVanilla(ingredients.toString())){
//do what you want
}
else{
//
}



then you write other similary methods like isCaffeVanilla, that works for others frappuccino types
Was This Post Helpful? 1
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: incorrect results from if statement

Posted 03 August 2013 - 09:33 AM

Or don't store the ingredients in a String. Use a String array. You can then use Arrays.asList().contains(foo);
Was This Post Helpful? 2
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: incorrect results from if statement

Posted 03 August 2013 - 11:26 AM

Or insert with delimiter and add delimters befor seraching

String x = "/milk/sugar/wine/scotch/"

String a1 = "/" + input.nextLine().toLowercase() + "/";
Was This Post Helpful? 0
  • +
  • -

#8 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 606
  • Joined: 10-March 13

Re: incorrect results from if statement

Posted 03 August 2013 - 12:05 PM

View Postg00se, on 03 August 2013 - 05:17 AM, said:

Your logic is wrong. The user will get congratulated if they enter six spaces - find out why.

You're right. Also, sometimes if I just keep entering a random letter it will print out the Congrats line. I found out why(since theres a space in the main string it takes your space as a correct answer), but I dont know how to fix it

View Postpbl, on 03 August 2013 - 04:49 AM, said:

cafeVanilla2 is already lowerCase no need to call lowerCase on it so many times that just make your code confusing
and never a good idea to call more than once the same method on the same object this

			a1 = input.nextLine().toLowerCase();
			a2 = input.nextLine().toLowerCase();
			a3 = input.nextLine().toLowerCase();
			a4 = input.nextLine().toLowerCase();
			a5 = input.nextLine().toLowerCase();


will simply rest of the code to

		if (caffeVanilla2.contains(a1) && 
		caffeVanilla2.contains(a2) &&
		caffeVanilla2.contains(a3) &&
		caffeVanilla2.contains(a4) &&
		caffeVanilla2.contains(a5)){


which is a lot simpler to understand and debug... and a lot less text to type :)/>/>

Ok thanks! Could I make it into an ignores case like:

a1 = input.nextLine().ignoreCase()

if (caffeVanilla2.contains(a1) && 
		caffeVanilla2.contains(a2) &&
		caffeVanilla2.contains(a3) &&
		caffeVanilla2.contains(a4) &&
		caffeVanilla2.contains(a5)){


This post has been edited by streek405: 03 August 2013 - 12:07 PM

Was This Post Helpful? 0
  • +
  • -

#9 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: incorrect results from if statement

Posted 03 August 2013 - 12:17 PM

ignoreCase() is not a function that I am aware of. Because your original answers you are looking for or lowercase, converting to lower case makes case a non-issue during your contains method. Is there any reason you will need the string in any other case?

This post has been edited by ConciselyVerbose: 03 August 2013 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#10 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2794
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: incorrect results from if statement

Posted 03 August 2013 - 12:23 PM

There's an equalsIgnoreCase isn't there? I haven't done Java in like 3 years but I want to help. :( Why not just put the ingredients into a string array then loop through to check the user inputs, that way you don't have to delimit and whatnot?
Was This Post Helpful? 1
  • +
  • -

#11 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 606
  • Joined: 10-March 13

Re: incorrect results from if statement

Posted 03 August 2013 - 12:26 PM

View PostConciselyVerbose, on 03 August 2013 - 12:17 PM, said:

ignoreCase() is not a function that I am aware of. Because your original answers you are looking for or lowercase, converting to lower case makes case a non-issue during your contains method. Is there any reason you will need the string in any other case?

Sorry, I meant isnt there a way to make the program ignore the case of whatever the user enters, in order to match it with the correct answer? Like lets say the correct answer, to match with is, hello, but for some reason the user enters HELLO or Hello.

View PostNeoTifa, on 03 August 2013 - 12:23 PM, said:

There's an equalsIgnoreCase isn't there? I haven't done Java in like 3 years but I want to help. :(/> Why not just put the ingredients into a string array then loop through to check the user inputs, that way you don't have to delimit and whatnot?

That is another good idea! Thanks
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: incorrect results from if statement

Posted 03 August 2013 - 12:33 PM

Quote

Why not just put the ingredients into a string array then loop through to check the user inputs, that way you don't have to delimit and whatnot?
.. which i'd already suggested, but with an easier way of searching that array
Was This Post Helpful? 0
  • +
  • -

#13 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2794
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: incorrect results from if statement

Posted 03 August 2013 - 12:52 PM

Whoops, sorry, didn't see that. :x
Was This Post Helpful? 0
  • +
  • -

#14 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: incorrect results from if statement

Posted 03 August 2013 - 12:54 PM

And yes there's a equalsIgnoreCase method you can use :)
Was This Post Helpful? 1
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: incorrect results from if statement

Posted 03 August 2013 - 01:07 PM

Quote

Whoops, sorry, didn't see that. :x
No problem ;)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2