Results 1 to 6 of 6
Thread: if else not working
- 05-12-2011, 05:49 PM #1
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
if else not working
Hi, my if else statement is not working. If I enter the name "Pawel", the program is supposed to output "Hello Pawel, I hope you are having a nice day!".
But when I enter Pawel as the name, it never goes to that line, it skips and outputs what is in the else block. Any help greatly appreciated. Thanks. Derek
here is the code.
Java Code://name and random number by Derek Van Derven import java.util.Scanner; //import scanner class specifically import java.util.Random;//import random class public class nameRandom { public static void main (String[] args) { Random generator = new Random(); int num1; num1 = generator.nextInt(100);//create random number between 0 and 99 String firstName; String lastName; Scanner scan = new Scanner (System.in); //create scanner obj, System.in is the keyboard. System.out.println ("Enter your first name."); firstName = scan.nextLine(); System.out.println ("Enter your last name"); lastName = scan.nextLine(); if (firstName == "Pawel" || firstName == "pawel") { System.out.println("Hello Pawel, I hope you are having a nice day!"); System.out.println("Your name is: " + firstName + " " + lastName + ". Thanks Pawel"); System.out.println("Here is a random number 0-99: " + num1); } else { System.out.println("Your name is: " + firstName + " " + lastName + "."); System.out.println("Here is a random number 0-99: " + num1); } } }
-
Don't use == to compare Strings since this checks to see if one String object is the same as another, something you're not interested in. Instead use String's equals(...) or equalsIgnoreCase(...) method which will check if the character sequences of the String objects are the same -- something you are interested in.
- 05-12-2011, 05:51 PM #3
Don't use == with Strings (or any Object unless you're testing whether the references are the same). Use the equals() method instead.
Edit- Too slow!How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
- 05-12-2011, 05:56 PM #5
You also might want to use .equalsIgnoreCase() that way you don't have to have a seperate check for Pawel, pAwel, paWel, etc...
- 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.
- 05-12-2011, 07:29 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
THANK YOU SO MUCH!!!! GOD I LOVE THIS FORUM. This is the best forum with the nicest people for any programming forum I have ever been on. And I receive the most help. Thank you all and fubarable I NEVER would have caught that. here is the new code snippet.
Java Code:if (firstName.equalsIgnoreCase("Pawel")) { System.out.println("Hello Pawel, I hope you are having a nice day!"); System.out.println("Your name is: " + firstName + " " + lastName + ". Thanks Pawel"); System.out.println("Here is a random number 0-99: " + num1); }
Similar Threads
-
Why isn't this working?
By nickburris in forum New To JavaReplies: 14Last Post: 02-03-2011, 02:00 AM -
\n not working in GUI (working code, but \n isn't working)
By cc11rocks in forum New To JavaReplies: 2Last Post: 01-04-2011, 04:30 AM -
working with JC
By yuhobebbho in forum New To JavaReplies: 0Last Post: 02-10-2010, 11:22 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
Working With ANT
By JavaForums in forum EclipseReplies: 0Last Post: 04-26-2007, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks