Results 1 to 4 of 4
  1. #1
    mochibon is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default Solved -While loop problem

    I am trying to make a while loop that checks to see if a name that is typed in matches one of 4 or 5 names and if it doesn't match, the while loop will return the user to try and type in a correct name.

    In pseudo code, I'm thinking it should say something like this:

    Enter a name
    While that name is not "John Doe" or "Alice White" or "Sam Green" or "Alex Timm" or "Jake Oriely", request that the person enter in a correct name.

    Else, continue on in the program.

    As far as I can tell my code says exactly this but despite whether I input a valid name or not, I get stuck in the while loop. Any recommendations?

    Java Code:
    Scanner scanner= new Scanner(System.in);
    System.out.println("Please input name as it appears in bank records");
    
    {String name = scanner.nextLine();
      System.out.println("" +name);
       while(name != "John Doe" && name != "Alice White" && name != "Sam Green" && name != "Alex Timm" && name !=  "Jake Oriely")//says loop back if not valid
      {
        System.err.println("Invalid name, please enter a valid name"); //error for invalid name
        name= scanner.nextLine();
      }
      
    System.out.println("Welcome, " +name);
    }
    Last edited by mochibon; 04-18-2010 at 09:16 PM. Reason: Solved

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    You can't compare Strings for (in)equality using the == or != operators. Read the API documentation for the String class and pay special attention to the equals( ... ) method.

    kind regards,

    Jos

  3. #3
    mochibon is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks so much, I found exactly what I needed and it runs great now!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by mochibon View Post
    Thanks so much, I found exactly what I needed and it runs great now!
    You're welcome of course.

    kind regards,

    Jos

Similar Threads

  1. For Loop Problem
    By saqib15 in forum New To Java
    Replies: 6
    Last Post: 02-20-2010, 01:59 AM
  2. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  3. While-loop problem
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 04:10 AM
  4. Help with a loop-like problem
    By Jnoobs in forum New To Java
    Replies: 1
    Last Post: 10-14-2009, 02:15 AM
  5. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 08:08 AM

Posting Permissions

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