# JAVA: Problem: Beginner Simple if else statement

Page 1 of 1

## 5 Replies - 3631 Views - Last Post: 09 July 2012 - 12:21 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=285199&amp;s=059424d01e5082531612244e313a57b7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 3
• Joined: 08-July 12

# JAVA: Problem: Beginner Simple if else statement

Posted 08 July 2012 - 11:16 PM

I'm learning the use of "if" and "else" statement on java.

----------------------------------------------------------------------------------

```import java.util.*;
public class Testing{
public static void main (String[]args){

Scanner input = new Scanner(System.in);

//Question 1

System.out.print("\n[1] If there are 3 apples and you take away 2, how many do you have?: \n");
System.out.print("[a] 1\n");
System.out.print("[b] 2\n");
System.out.print("[c] 3\n");
System.out.print("[d] 4\n");
String ans = input.next();

if (ans == "b")
System.out.println("WRONG! The answer is: [a] 1.\nBecause 3 - 2 = 1");

else if (ans=="a" || ans=="c" || ans=="d")

System.out.println("WRONG! The answer is: [b] 2\n(You should know, you just took them!)");

else
System.out.println("WRONG! The answers should be [a b c d] only!");

}
}
```

Whenever I chooses a, b, c or d, I always get this message:
"WRONG! The answers should be [a b c d] only!"
P.S. If I may to add, Please include the user can't proceed to to next Question unless he/she type a, b, c or d.
THANKS!

Is This A Good Question/Topic? 0

## Replies To: JAVA: Problem: Beginner Simple if else statement

### #2 fromTheSprawl

• Monomania

Reputation: 514
• Posts: 2,063
• Joined: 28-December 10

## Re: JAVA: Problem: Beginner Simple if else statement

Posted 08 July 2012 - 11:40 PM

You should use .equals instead of == for comparing Strings that way. When you use the latter it compares the objects while the former compares the values of the array. The use of == is okay though for primitive types such as int and char.

Reputation: 0
• Posts: 3
• Joined: 08-July 12

## Re: JAVA: Problem: Beginner Simple if else statement

Posted 08 July 2012 - 11:58 PM

fromTheSprawl, on 08 July 2012 - 11:40 PM, said:

You should use .equals instead of == for comparing Strings that way. When you use the latter it compares the objects while the former compares the values of the array. The use of == is okay though for primitive types such as int and char.

WORKS LIKE A charm! THANKS TheSprawl!
If I may to add, how will I loop back the question when the user type the wrong input instead only [a, b, c or d]?

### #4 fromTheSprawl

• Monomania

Reputation: 514
• Posts: 2,063
• Joined: 28-December 10

## Re: JAVA: Problem: Beginner Simple if else statement

Posted 09 July 2012 - 12:01 AM

Use a while loop. Declare a boolean that will escape that loop if true. Like this:

```boolean isCorrect = false;
do{
//Ask what is the first lowercase letter of the alphabet
isCorrect = true;
}
}while(isCorrect==false);

```

Reputation: 0
• Posts: 3
• Joined: 08-July 12

## Re: JAVA: Problem: Beginner Simple if else statement

Posted 09 July 2012 - 12:11 AM

fromTheSprawl, on 09 July 2012 - 12:01 AM, said:

Use a while loop. Declare a boolean that will escape that loop if true. Like this:

```boolean isCorrect = false;
do{
//Ask what is the first lowercase letter of the alphabet
isCorrect = true;
}
}while(isCorrect==false);

```

fromTheSprawl YOUR MY HERO. You make my day complete!

### #6 fromTheSprawl

• Monomania

Reputation: 514
• Posts: 2,063
• Joined: 28-December 10

## Re: JAVA: Problem: Beginner Simple if else statement

Posted 09 July 2012 - 12:21 AM

Hey, no problem, I have your same problems when I was studying too. Good luck.