Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Testing if an inputted String exists in an String Array

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Testing if an inputted String exists in an String Array

    Ok,

    So what i want to do is read in a String from the user and test to see if the inputted String is equal to one of the Strings that exist in a String array that i have created.

    Here is what i have:

    public class task13 {
     
        public static void main(String[] args) {
     
            String[] passwords = new String[]{"rose77", "2today", "5staff"};
     
            Scanner scan = new Scanner(System.in);
     
            System.out.println("Enter a valid company password: ");
     
            String input = scan.nextLine();
     
           [B] if (input is equal to one of the Strings in the String Array)
     
                      then execute this
     
             else ...........................[/B]
        }
    }


    I have highlighted the bold part which is where i am stuck im not sure which class or method i need to use.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Testing if an inputted String exists in an String Array

    Look at the String class. It has several methods for comparing Strings.
    You will need a loop that accesses each element in the array and tests that element.
    You'll also need a way to remember the results when the code exits the loop so the following code can react as needed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Testing if an inputted String exists in an String Array

    Ok cheers,

    had a quick look around and a quick think and only thing i came up with is this:

     public static void main(String[] args) {
     
            String[] passwords = new String[]{"rose77", "2today", "5staff"};
     
            Scanner scan = new Scanner(System.in);
     
            System.out.println("Enter a valid company password: ");
     
            String input = scan.nextLine();
     
            if (input.equals(passwords[0])){
                System.out.println("Password Validated");
            }
            else if (input.equals(passwords[1])){
                System.out.println("Password Validated");
            }
            else if (input.equals(passwords[2])){
                System.out.println("Password Validated");
            }
            else {
                System.out.println("Invalid Password!");
            }
        }
    }


    As you can see it checks each index of the String array one by one using the .equals() method.


    My only concern with this way is that if there were 100 items in the Array, that would one hell of an if-else statement to type out and check each individual one.

    So i'm wondering is there a more practical way of doing it ?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Testing if an inputted String exists in an String Array

    The code needs to use a for loop with an index variable. It should NOT index each element by explicit index.
     if (input.equals(passwords[0])){
    [0] is the wrong way to do it. Put the test in a loop and use the loop's variable: [i]

    You seem to have missed most of what I said in post#2
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  2. Replies: 3
    Last Post: October 26th, 2012, 02:19 PM
  3. Print 000 infront of String, I can get INT to work but not string
    By keat84 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 1st, 2012, 11:23 PM
  4. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM