if problem

Put your problem here if it does not fit any of the other categories.

if problem

Postby microhome » Tue May 19, 2009 2:29 am

Hi everybody,
I use the following if-clause. "answer" is "1" but I get the message: "wrong response". Can you tell me what's wrong?!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if (answer == "1"){
  2.  
  3.      Toast.makeText(SolocateMain.this, "right response", Toast.LENGTH_SHORT).show();
  4.  
  5. }
  6.  
  7. else{
  8.  
  9.      Toast.makeText(SolocateMain.this, "wrong response", Toast.LENGTH_SHORT).show();
  10.  
  11. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
microhome
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun May 10, 2009 8:49 pm
Location: Berlin, Germany

Top

Postby arams » Tue May 19, 2009 10:52 am

String answer="1";
if (answer == "1"){
Toast.makeText(this, "right response", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "wrong response", Toast.LENGTH_SHORT).show();
}


:( Check the datatype....
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby nmc » Tue May 19, 2009 11:39 am

Using Java, you cant compare strings using "==";
try answer.equals("1") instead.
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby microhome » Tue May 19, 2009 12:25 pm

Thanks for your answer! You were right, it is not possible, comparing strings that way. So I decided to convert that string to int:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int answer = Integer.valueOf( sb.toString() ).intValue();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


It works fine!
microhome
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun May 10, 2009 8:49 pm
Location: Berlin, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 24 guests