# If Statement Never Gets Called (SUPER EASY!)

• 10-16-2011, 03:17 AM
skaterboy987
If Statement Never Gets Called (SUPER EASY!)
Okay so, no matter what the results of this coin toss, The user's money never goes up.
What i want is if the results are HHH your money should be 3 * The money specifed for this
toss.

So:
Example:

How many coin tosses would you like to make 1-50?
20
---------------------------------
How much money is each toss worth in \$?
2
Each toss will be worth 2\$
---------------------------------
Do you want Heads(H) or Tails(T)
H
H Has been chosen
---------------------------------
Here are the results:
H H H T T T T T T H H T T T H T T H H H

Is all I want.
But every single time it always says your money is 0 :(
here's my code
Code:

```import java.util.Random; import java.util.Scanner; public class Coing_Toss {         public static void main (String args[]){                 String  CpuChoice = null;                 int Hcount = 0;                 int Tcount = 0;                 int Money = 0;                 Scanner scanner = new Scanner(System.in);                         System.out.println("How many coin tosses would you like to make 1-50?");                 String Tosses = scanner.nextLine();                 int ParseTosses = Integer.parseInt(Tosses);         System.out.println(Tosses + " Tosses will be made");         System.out.println("---------------------------------");         System.out.println("How much money is each toss worth in \$?");                 String Tossworth = scanner.nextLine();                 int ParseTossworth = Integer.parseInt(Tossworth);         System.out.println("Each toss will be worth " + Tossworth + "\$");         System.out.println("---------------------------------");         System.out.println("Do you want Heads(H) or Tails(T)");                 String Choice = scanner.nextLine();         System.out.println(Choice + " Has been chosen");         System.out.println("---------------------------------");                 System.out.println("Here are the results:");         for(int i=1; i<(ParseTosses+1); i++)         {                 Random randomGenerator = new Random();                   int RandomInt = randomGenerator.nextInt(2);                   if (RandomInt == 1)                                   {                         CpuChoice = "H";                         Hcount = Hcount + 1;                         System.out.print("H ");                                   }                                   else                                   {                           CpuChoice = "T";                           Tcount = Tcount + 1;                           System.out.print("T ");                                   }                                                     }                 if (Choice == "T")         {                 Money = (ParseTossworth * Tcount);         }                 if (Choice == "H")         {                 Money = (ParseTossworth + Hcount);         }                 System.out.println("");         System.out.println(Hcount + " Heads and " + Tcount + " Tails");                  System.out.println("Your money is " + Money);                                             } }```
Thanks for the help!
• 10-16-2011, 03:41 AM
Fubarable
Re: If Statement Never Gets Called (SUPER EASY!)
Don't compare Strings like this:

Code:

`if (Choice == "T") {`

Instead you'll want to use the equals or equalsIgnoreCase method:

Code:

`if (Choice.equals("T")) {`

The problem is the first code compares if one String object is the same as another, and you really don't care if this is true or not. Instead you are interested in knowing if both Strings hold the same characters in the same order which is what the equals/equalsIgnoreCase check.
• 10-16-2011, 03:42 AM
pbrockway2
Re: If Statement Never Gets Called (SUPER EASY!)
Code:

`if (Choice == "T")`
Don't compare strings (or other objects) with ==, instead use their equals() method. The reason is that each class defines equals() in a way that makes sense for that class. For instance the String class equals() will return true when the strings are made up of the same characters in the same order: even if they are different instances of that string.

-----

Please use standard Java coding conventions. Variables should start with a lowerCaseLetter and camel case used where the variable is made up of multiple words. So choice, hCount etc. The more descriptive the variable, the better, so hCount might be better as headCount or numHeads. The aim is to make the code as readable (and self documenting) as possible.
• 10-16-2011, 03:44 AM
skaterboy987
Re: If Statement Never Gets Called (SUPER EASY!)
Thanks so much!
Works like a charm!