Results 1 to 5 of 5
  1. #1
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Issue with scanners and strings

    Hi everyone,
    So I have written a code which allows for a user to input a name through the use of a scanner and then using a while statement, an array is searched through to see if the name already exists within the array. My problem here is that whenever I input a name into the scanner that I know is on the array the code seems to disregard it and report that the name does not exist within the array. Any help would be appreciated.

    import java.util.*;

    public class test
    {
    private static int size = 20;
    private String customerName[];

    public void customerName()
    {
    customerName = new String[size];
    customerName[0] = "Joshua";
    customerName[1] = "Sandee";
    customerName[2] = "Sam";
    customerName[3] = "Laure";
    customerName[4] = "Joseph";
    int place = 0;
    String name;
    Scanner in = new Scanner(System.in);
    System.out.print("What is the customers name:");
    name = in.nextLine();
    System.out.print("Your name is " +name);
    while (place <customerName.length && name != customerName[place])
    {
    place++;
    //This is just in place to make sure the while statment goes through the entire array
    System.out.println("Hi");
    }
    if (place == customerName.length)
    {
    System.out.println("Unknown member.");
    }
    else
    {
    System.out.print("Already a member.");
    }
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Issue with scanners and strings

    Java Code:
    name != customerName[place]
    You compare objects (and Strings are objects) using equals(), not == or !=.

  3. #3
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Issue with scanners and strings

    Oh okay thank you and one more question is there an equivlaent of != too in words, if you understand what I'm asking. Sorry I'm still only starting Java.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Issue with scanners and strings

    !someString.equals(someOtherString)

  5. #5
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Issue with scanners and strings

    Thank you so very much :)

Similar Threads

  1. Problem with Scanners
    By Sharoo in forum New To Java
    Replies: 2
    Last Post: 11-11-2011, 05:17 AM
  2. Strings, scanners and IFs
    By JavaGame in forum New To Java
    Replies: 4
    Last Post: 09-07-2011, 11:58 PM
  3. Replies: 6
    Last Post: 08-22-2011, 05:32 AM
  4. Buffered Reader and scanners
    By nevermiind in forum New To Java
    Replies: 8
    Last Post: 05-04-2010, 06:22 PM
  5. Scanners / Printers
    By cytech in forum New To Java
    Replies: 0
    Last Post: 03-17-2008, 05:54 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
  •