# Thread: If Statement Never Gets Called (SUPER EASY!)

1. Member Join Date
Sep 2011
Posts
56
Rep Power
0

## 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
20 Tosses will be made
---------------------------------
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
9 Heads and 11 Tails
Your money is 18

Is all I want.
But every single time it always says your money is 0 :(
here's my code
Java 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!  Reply With Quote

2. ## Re: If Statement Never Gets Called (SUPER EASY!)

Don't compare Strings like this:

Java Code:
`if (Choice == "T") {`

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

Java 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.  Reply With Quote

3. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## Re: If Statement Never Gets Called (SUPER EASY!)

Java 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.  Reply With Quote

4. Member Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: If Statement Never Gets Called (SUPER EASY!)

Thanks so much!
Works like a charm!  Reply With Quote

#### Posting Permissions

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