Results 1 to 4 of 4
Thread: Testing for single string in IF
- 01-31-2011, 06:17 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Testing for single string in IF
I'm running a program and a part of it requires the user to enter Y/N to a question can anyone tell me if this test method is even the slightest bit correct for conducting such a test...
[cod]
public static boolean isAnswer(String p){
if(p=="Y"){
return true;
}else{
return false;
}//close if-else statement
}//close isAnswer method
[/cod]
I'm simply testing true or false whether the user entered Y to continue the loop of the program or to end it. Thanks alot.
- 01-31-2011, 06:33 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Don't use == to compare strings; this compares object references, not the content of the string. Use the equals() method:
Java Code:if(p.equals("Y")) ...
- 01-31-2011, 07:05 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
IRON LION, thanks much bro...
That's exactly what I was missing, i'm suprised though I missed something simple as that but I guess when you're deep in heavy code sometimes the small things miss us.
Thanks again pal...
- 01-31-2011, 07:21 PM #4
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
Similar Threads
-
Testing if it is an Int and if not ask again
By Alexander_003 in forum New To JavaReplies: 2Last Post: 11-27-2010, 12:09 AM -
how to change a single index of a string
By ftrengnr in forum New To JavaReplies: 5Last Post: 11-26-2010, 04:08 AM -
how to find more than one String with single command
By doha786 in forum New To JavaReplies: 2Last Post: 03-27-2010, 07:22 PM -
Splitting single string into array elements
By phil128 in forum New To JavaReplies: 11Last Post: 01-12-2009, 11:51 AM -
Testing EMF models.
By manik_jforum in forum EclipseReplies: 0Last Post: 12-10-2008, 08:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks