Results 1 to 4 of 4
- 03-27-2012, 12:06 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Why isn't this if statement working?
I tried experimenting with scanners and if statements, and it didn't work. I can't really figure out why. (very new to java, don't judge me)
Any ideas why it isn't working? I assume it's pretty obvious, so it's gonna bother me when I find out. <_<Java Code:import java.util.Scanner; class NewEmpty5{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("What's your name?"); String name = scanner.next(); if(name == "Not Available"){ System.out.println("I'm sorry, we aren't allowing anybody to use the name \"Not Available\""); }else if(name != "Not Available"){ System.out.println("Thank you for not choosing the name \"Not Available\""); } } }
Output when I typed "Not Available":
run:
What's your name?
Not Available
Thank you for not choosing the name "Not Available"
BUILD SUCCESSFUL (total time: 4 seconds)
Thanks in advance.Last edited by ocomobock; 03-27-2012 at 12:13 AM.
- 03-27-2012, 12:52 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Re: Why isn't this if statement working?
Don't use == and != when comparing things other than primitive values (numbers etc). Instead use the the equals() method. Every class defines equals() in some way that makes sense for it: in the case of strings they are equal when they are made up of the same characters in the same order.
name.equals("whatever") is a boolean expression: that is it will be a boolean value (true or false). I mention this because you may want to use the ! operator in conjunction with such an expression to express not being equal to.Java Code:if(name.equals("Not Available")) {
- 03-27-2012, 01:37 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Why isn't this if statement working?
Thank you! I know how to use the equals() method but some reason it just didn't cross my mind.
EDIT: I edited the code and now I have another problem. How do I make the program remember that you can't use that name?
Output:Java Code:import java.util.Scanner; class NewEmpty5{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("What's your name?"); String name = scanner.next(); if(name.equals("NotTelling")){ System.out.println("I'm sorry, we aren't allowing anybody to use the name \"NotTelling\""); System.out.println("What's your name?"); scanner.next(); } System.out.println("Hello, " + name); } }
run:
What's your name?
NotTelling
I'm sorry, we aren't allowing anybody to use the name "NotTelling"
What's your name?
NotTelling
Hello, NotTelling
BUILD SUCCESSFUL (total time: 7 seconds)Last edited by ocomobock; 03-27-2012 at 02:03 AM.
- 03-27-2012, 02:13 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Similar Threads
-
'if' statement not working?
By Etimer in forum New To JavaReplies: 4Last Post: 03-10-2012, 05:18 AM -
If statement is not working properly
By Alpa in forum New To JavaReplies: 3Last Post: 02-04-2012, 08:40 PM -
SQL statement working in one class but not another
By Dcalladi in forum New To JavaReplies: 4Last Post: 11-11-2011, 01:35 AM -
if statement with strings not working... again
By hardcorebadger in forum New To JavaReplies: 4Last Post: 01-11-2011, 06:02 AM -
Update statement not working.
By OMFGITSROHIT in forum JDBCReplies: 5Last Post: 04-08-2010, 01:03 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks