Results 1 to 10 of 10
  1. #1
    ameerulislam is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Problem with Head/Tails program

    I just wrote head or tails program. The program is working when I'm putting heads but its giving me wrong message when I put Tails. I spent a lot of time behind it and I can't find the silly mistake that I made :(.


    Please help me out..

    Thanks



    Java Code:
    import java.util.Scanner;
    public class problem3p14 {
    
    	/**
    	 * problem3.14
    	 */
    	public static void main(String[] args) {
    		int usernumber=0;
    		 System.out.println("Enter a guess (Head/ Tail): ");
    		 Scanner input = new Scanner(System.in);                    // user input variable is a string input 
    	     String answer = input.next();
    	    
    	     int number1 = (int)(System.currentTimeMillis() % 2);  
    	    
    	     if(answer=="Head"||answer=="head"){                        // converting Head into 0
    	    	usernumber=0;
    	    	 
    	     }else if(answer=="Tail"||answer=="tail"){                 // converting Tail into 1
    	    	 usernumber=1;
    	     }
    	      
    	     if(number1==usernumber){                                   //if matched get into this one
    	    	 
    	    	 if(usernumber==0){
    	    		 System.out.println("You Are Correct Its Heads");
    	    	 }else if (usernumber==1) {
    	    		 System.out.println("You Are Correct Its Tails");
    	    	 }	    	 
    	     }else{                                                     //if not matched get into this one
    	    	 if(usernumber==0){
    	    		 System.out.println("Oops! You got it wrong it was Tails");
    	    	 }else if (usernumber==1) {
    	    		 System.out.println("Oops! You got it wrong it was Head");
    	    	 }
    	     }
    	     input.close();
    }
    }

  2. #2
    DrummondAW is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Head/Tails program

    I think you need to do some debugging. Your if statements that compare the initial input and set your usernumber variable weren't working. To figure this out you simply set usernumber = 10 initially and then see if the value changes by executing a System.out.println. Try comparing your strings as such...

    if(answer.equals("Head")||answer.equals("head"))

    I'm still new to Java and I'm hoping someone can explain why the code doesn't work if you use == to compare the strings.

    Edit: found a good article explaining this problem
    Last edited by DrummondAW; 11-02-2012 at 04:39 AM.

  3. #3
    ameerulislam is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problem with Head/Tails program

    yes I have checked that, there is something wrong with the initialization. I tried putting 5 and program got broken actually. I forgot to mention that.

  4. #4
    DrummondAW is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Head/Tails program

    It should be working now, correct?

  5. #5
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default Re: Problem with Head/Tails program

    Just another small tip. instead of:
    if(answer.equals("Head")||answer.equals("head"))


    try:

    if(answer.equalsIgnoreCase("head"))

    instead. then you dont need to do the || check.

    Good luck

  6. #6
    ameerulislam is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problem with Head/Tails program

    Quote Originally Posted by DrummondAW View Post
    It should be working now, correct?
    Guys its working with .equals and .equalsIgnoreCase! Strange really.. why this one didn't work with == .

    Thanks both of you.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    10,484
    Rep Power
    16

    Default Re: Problem with Head/Tails program

    Strings are objects.
    You compare them with equals().
    '==' simply checks whether it is referring to the exact same object on the heap.
    Please do not ask for code as refusal often offends.

  8. #8
    ameerulislam is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problem with Head/Tails program

    if strings are objects, numbers such as are int double are what?

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    10,094
    Rep Power
    17

    Default Re: Problem with Head/Tails program

    Primitives.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    10,094
    Rep Power
    17

    Default Re: Problem with Head/Tails program

    To further clarify; variable references of a primitive type actually hold the value assigned to them. So if two int variables a and b both contain the value 5
    Java Code:
    int a = 5;
    int b = 5;
    it follows that the two variables have an equal content and this evaluates to true:
    Java Code:
    a == b
    Variables of a reference type -- those that refer to obejcts -- hold a reference to the assigned value. Thus for
    Java Code:
    String a = new String("Hello");
    String b = new String("Hello");
    the two variables hold different references and this evaluates to false:
    Java Code:
    a == b
    The equals(...) method passes a reference to one String to the other String to determine its equality; like any other method, the equals(...) method executes the program flow determined by code within the method and returns a value. To see what String#equals(...) really does, take a look at the source of java.lang.String which you can find in the src.zip file in your JDK installation folder.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. Head First Java Book question
    By silverglade in forum New To Java
    Replies: 4
    Last Post: 05-02-2011, 09:23 PM
  2. Help: " no pulse-java" problem - Head First Java
    By kmckinley820 in forum Java Applets
    Replies: 2
    Last Post: 02-25-2011, 07:13 PM
  3. Monitor Disc Head Using Java
    By zorroforce in forum New To Java
    Replies: 4
    Last Post: 09-18-2009, 07:51 AM
  4. HEAD first by kathy sierra
    By j2vdk in forum New To Java
    Replies: 0
    Last Post: 08-30-2008, 01:08 PM
  5. Replies: 2
    Last Post: 11-11-2007, 08:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •