5 Replies - 3215 Views - Last Post: 09 July 2012 - 12:21 AM Rate Topic: -----

#1 hanadori  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • 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.
Was This Post Helpful? 2
  • +
  • -

#3 hanadori  Icon User is offline

  • New D.I.C Head

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

Re: JAVA: Problem: Beginner Simple if else statement

Posted 08 July 2012 - 11:58 PM

View PostfromTheSprawl, 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]?
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • 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
if(answer.equals("a")){
isCorrect = true;    
}
}while(isCorrect==false);


Was This Post Helpful? 1
  • +
  • -

#5 hanadori  Icon User is offline

  • New D.I.C Head

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

Re: JAVA: Problem: Beginner Simple if else statement

Posted 09 July 2012 - 12:11 AM

View PostfromTheSprawl, 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
if(answer.equals("a")){
isCorrect = true;    
}
}while(isCorrect==false);


fromTheSprawl YOUR MY HERO. You make my day complete! :D
Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • 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. ^^
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1