Results 1 to 6 of 6
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default 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);
    					}
    				
    		}
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    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!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by KevinWorkman View Post
    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!
    Ha! :)

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  6. #6
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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

  1. Why isn't this working?
    By nickburris in forum New To Java
    Replies: 14
    Last Post: 02-03-2011, 02:00 AM
  2. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  3. working with JC
    By yuhobebbho in forum New To Java
    Replies: 0
    Last Post: 02-10-2010, 11:22 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •