Results 1 to 10 of 10
Thread: Problem with Head/Tails program
- 11-02-2012, 03:39 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
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(); } }
- 11-02-2012, 04:31 AM #2
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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 problemLast edited by DrummondAW; 11-02-2012 at 04:39 AM.
- 11-02-2012, 04:43 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
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.
- 11-02-2012, 05:11 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
Re: Problem with Head/Tails program
It should be working now, correct?
- 11-02-2012, 05:30 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
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
- 11-02-2012, 09:29 AM #6
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
- 11-02-2012, 10:47 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 11-03-2012, 12:14 PM #8
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Problem with Head/Tails program
if strings are objects, numbers such as are int double are what?
- 11-03-2012, 01:55 PM #9
Re: Problem with Head/Tails program
Primitives.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-03-2012, 02:02 PM #10
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
it follows that the two variables have an equal content and this evaluates to true:Java Code:int a = 5; int b = 5;
Variables of a reference type -- those that refer to obejcts -- hold a reference to the assigned value. Thus forJava Code:a == b
the two variables hold different references and this evaluates to false:Java Code:String a = new String("Hello"); String b = new String("Hello");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.Java Code:a == b
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Head First Java Book question
By silverglade in forum New To JavaReplies: 4Last Post: 05-02-2011, 09:23 PM -
Help: " no pulse-java" problem - Head First Java
By kmckinley820 in forum Java AppletsReplies: 2Last Post: 02-25-2011, 07:13 PM -
Monitor Disc Head Using Java
By zorroforce in forum New To JavaReplies: 4Last Post: 09-18-2009, 07:51 AM -
HEAD first by kathy sierra
By j2vdk in forum New To JavaReplies: 0Last Post: 08-30-2008, 01:08 PM -
Problem calling classes to flip coin x number of times and record heads or tails
By adlb1300 in forum New To JavaReplies: 2Last Post: 11-11-2007, 08:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks