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: Can't think of a more specific title than: 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

    Default [SOLVED] Can't think of a more specific title than: Problem with if statements

    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:16 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Can't think of a more specific title than: Problem with if statements

    See post on your other thread for the same problem

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Can't think of a more specific title than: Problem with if statements

    See the message I sent you

  4. #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: Can't think of a more specific title than: Problem with if statements

    Quote Originally Posted by Tjstretch View Post
    See the message I sent you
    I can't seem to find your message anywhere.

  5. #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: Can't think of a more specific title than: Problem with if statements

    Solved with .equals()
    Quote Originally Posted by Norm View Post
    Use the equals() method when comparing the contents of objects, like Strings.
    Check out Java: ==, .equals(), compareTo(), and compare() for more info on .equals() .

Similar Threads

  1. Problem with if statements
    By Omnamah in forum Loops & Control Statements
    Replies: 4
    Last Post: September 4th, 2011, 11:13 AM
  2. Problem-specific design question
    By olsonpm in forum Object Oriented Programming
    Replies: 6
    Last Post: July 20th, 2011, 02:00 PM
  3. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 03:26 PM
  4. About Title border in jscrollpane
    By subhvi in forum AWT / Java Swing
    Replies: 2
    Last Post: December 14th, 2009, 11:45 PM
  5. JFrame, frame's title doesnt appear.
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 26th, 2009, 02:38 PM

Tags for this Thread