Results 1 to 8 of 8
Thread: String If Statements
- 06-22-2011, 03:06 AM #1
String If Statements
Hello, I have recently encountered a problem where I'm not sure exactly how to do an if statement in check for strings. I do know how to check for numbers just for some reason I can't seem to figure this out.
I thought it would be something like
if(fn == "hello){
System.out.println("Whats Up?");
}
but that didn't work
- 06-22-2011, 03:10 AM #2
This is one of the most common problems encountered by n00bs and one the most often asked questions. Use the equals method instead. Do a search and find the 1000000000 times it has been explained.
- 06-22-2011, 03:26 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Use code tags when posting codes.
As what junky said use equals() method:
Java Code:if("hello".equals(aString)) { //your code }
- 06-22-2011, 07:34 AM #4
- 06-22-2011, 07:35 AM #5
My finger got tired!
- 06-22-2011, 09:00 AM #6
- 06-22-2011, 10:08 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 26
- Rep Power
- 0
if(fn.equals("hello"){
System.out.println("Whats Up?");
}
this code is work because == is used for same object but two reference are pointing to same object that time use == operator.
- 06-22-2011, 03:23 PM #8
To clarify what jatinkansagara said, you would use the == operator to test if two object references reference the same object. See example.
Java Code:class MyProgram { public static void main(String[] args) { Object foo1 = new Object(); Object foo2 = new Object(); Object foo3 = foo1; if(foo1 == foo3) System.out.println("Foo1 does equal Foo3!"); if(foo1 == foo2) System.out.println("Foo1 does equal Foo2!"); else System.out.println("Foo1 does not equal Foo2!"); } }- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
Similar Threads
-
Help with if else statements
By np2392 in forum New To JavaReplies: 2Last Post: 09-24-2010, 01:25 AM -
if else statements
By sweetpea123 in forum New To JavaReplies: 4Last Post: 04-12-2010, 07:02 PM -
age: using if statements
By yasmin k in forum New To JavaReplies: 2Last Post: 10-04-2009, 09:50 PM -
Help with if-else statements
By porchrat in forum New To JavaReplies: 4Last Post: 03-23-2009, 04:24 PM -
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 07:56 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks