Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with if statements

  1. #1
    Junior Member Omnamah's Avatar
    Join Date
    Sep 2011
    Location
    England/Wales
    Posts
    29
    My Mood
    Inspired
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Problem with if statements

    MOVED TO Loops & Control Statements SUB-FORUM
    http://www.javaprogrammingforums.com...html#post41860

    So I was sitting in the living room last night on my laptop and my sister was watching some rubbish on the television. A show called something like 'Red & Blue'. The basis of the game is that people choose either a red or a blue box and depending on which one they choose, they win or lose. I was in awe at how melodramatic the whole show was and how it was generally ridiculous. It led me to think "Hey! That seems simple enough, I could code a program to do that!" Which leads me onto the Java

    I can't get my head around what on Earth is wrong with this code. I think the problem lies in the first if statement, but I can't work out what it is. It's especially odd that the error message that I set up as the 'else' of that statement also outputs. There are no compiler errors thrown, so it's compiling ok (duh), but just not doing what I want it to.

    I've sat staring at the screen for ages but can't work it out. This problem has thus been the catalyst for me finally joining a dedicated java forum. Any help would be fantastic, thank you!

    import java.util.Scanner;
    import java.util.Random;
     
    class apples {
    	 public static void main(String[] args) {
     
    	     Scanner input = new Scanner(System.in);
    		 Random dice = new Random();
     
    		 String userGuess;
     
     
    		 System.out.println("Which will it be? Red or blue? Enter your guess NOW! (r/b): ");
    		 userGuess = input.nextLine();
     
     
    		 int userGuessInt = 0;
     
    		if (userGuess == "r") {
    			 userGuessInt = 0;
    		 }
    		 else if (userGuess == "b"){
    			 userGuessInt = 1;
    		 }
    		 else {
    			 System.err.println("You were supposed to guess either \"b\" or \"r\", nigga!");
     
    		 }
     
     
    		 int number = dice.nextInt(2);
     
     
    		 if (number == 0){
    			 System.out.println("It's red!");
    		 }
    		 else {
    			 System.out.println("It's blue!");
    		 }
     
     
    		 if (number == userGuessInt){
    			 System.out.println("You win!");
    		 }
    		 else {
    			 System.out.println("You lose! Now go cry over a fucking gameshow like a freaking idiot...");
    		 }
     
     
    	    }
    			}

    Output:
    Which will it be? Red or blue? Enter your guess NOW! (r/b): 
    r
    It's blue!
    You lose! Now go cry over a fucking gameshow like a freaking idiot...
    You were supposed to guess either "b" or "r", nigga!
    Last edited by Omnamah; September 4th, 2011 at 11:14 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with if statements

    if (userGuess == "r") {
    Use the equals() method when comparing the contents of objects, like Strings.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Omnamah (September 4th, 2011)

  4. #3
    Junior Member Omnamah's Avatar
    Join Date
    Sep 2011
    Location
    England/Wales
    Posts
    29
    My Mood
    Inspired
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with if statements

    *Searches 'equals()' in the API*
    I'll give it a shot, ta.

  5. #4
    Junior Member Omnamah's Avatar
    Join Date
    Sep 2011
    Location
    England/Wales
    Posts
    29
    My Mood
    Inspired
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with if statements

    Just found this for anyone else having a similar problem: Java: ==, .equals(), compareTo(), and compare()

  6. #5
    Junior Member Omnamah's Avatar
    Join Date
    Sep 2011
    Location
    England/Wales
    Posts
    29
    My Mood
    Inspired
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with if statements

    Worked like a charm.

Similar Threads

  1. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM
  2. Breaking If/Else Statements
    By aussiemcgr in forum Loops & Control Statements
    Replies: 9
    Last Post: August 27th, 2010, 03:28 PM
  3. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM
  4. continue statements
    By monroe in forum Java Applets
    Replies: 1
    Last Post: March 20th, 2010, 06:26 PM
  5. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM

Tags for this Thread