user6359011
user6359011

Reputation:

How do I check if user input is found in array?

I'm trying to check if user input is equal to the data I have in my arrays, then printing the int array that corresponds with the string array, but nothing appears when I run the program.

What am I doing wrong?

This is everything I have in my main method:

  String firstname;
  String middleinitial;

  System.out.println("What is your first name?");
  firstname = myScan.nextLine();

  System.out.println("What is your middle initial?");
  middleinitial = myScan.nextLine();

 String[] firstNameLetter = {"A", "Albert", "Alice", "Ann", "Anna", "Annie",        "Arthur", 
            "B", "Bernard", "Bette", "Bettie", "Betty", "C", "Carl",
            "Catherine", "Charles", "Clara", "D", "Donald", "Dorothy, E",
            "Edward", "Elizabeth", "F", "Florence", "Frank", "G", "George",
            "Grace", "H", "Harold", "Harriet", "Harry", "Hazel", "Helen",      "Henry",
            "I", "J", "James", "Jane", "Jayne", "Jean", "John", 
            "Joan", "Joseph", "K", "L", "M", "Margaret", "Martin",
            "Marvin", "Mary", "Melvin", "Mildred", "N", "O", "P",
            "Patricia", "Paul", "Q", "R", "Richard", "Ruby", "Robert",
            "Ruth", "S", "T", "Thelma", "Thomas", "U", "V",
            "W", "Walter", "Wanda", "William", "Wilma", "X", "Y",
            "Z"};

    int[] firstNameNumber     = {000, 020, 020, 040, 040, 040, 040,
             060, 80, 80, 80, 80, 100, 120,
             120, 140, 140, 160, 180, 180, 200,
             220, 220, 240, 260, 260, 280, 300,
             300, 320, 340, 340, 360, 360, 380, 380,
             400, 420, 440, 440, 440, 460, 460,
             480, 480, 500, 520, 540, 560, 560,
             580, 580, 600, 600, 620, 640, 660,
             680, 680, 700, 720, 740, 740, 760,
             760, 780, 800, 820, 820, 840, 860,
             880, 900, 900, 920, 920, 940, 960,
             980};

String[] middleNameLetter = {" ", "A", "B", "C", "D", "E", "F",
             "G", "H", "I", "J", "K", "L", "M",
             "N", "O", "P", "Q", "R", "S", "T",
             "U", "V", "W", "X", "Y", "Z"};

int[] middleNameNumber    = {0, 1, 2, 3, 4, 5, 6,
                             7, 8, 9, 10, 11, 12, 13,
                             14, 14, 15, 15, 16, 17, 18,
                             18, 18, 19, 19, 19, 19};

for (int count = 0; count < firstNameLetter.length; count++)
{
    if (firstname.equals(firstNameLetter[count]))
    {
        System.out.println(firstNameNumber[count]);
    }
}

for (int count = 0; count < middleNameLetter.length; count++)
{
    if (middleinitial.equals(middleNameLetter[count]))
    {
        System.out.println(middleNameNumber[count]);
    }
}

Upvotes: 0

Views: 9731

Answers (6)

bakely
bakely

Reputation: 59

if(str.toLowerCase().equals())

Upvotes: 0

Daniel Widdis
Daniel Widdis

Reputation: 9091

Your program is working correctly (other than the missing Scanner myScan = new Scanner(System.in); that you likely have and didn't repeat).

What may be confusing you is that you've defined several of your integers as octal values. When you begin an integer with a 0 value it is declared as an octal.

For example, the second value, corresponding to Albert, is 020. This is an octal value for 16.

As others have mentioned, you likely want to use equalsIgnoreCase() on your strings and break; from inside the loops. Also having two equal length arrays is dangerous; much better to store the values in pairs in a HashMap or other structure.

Upvotes: 0

Monny Star
Monny Star

Reputation: 83

When I ran this code myself all seemed to be working. I see nothing wrong with your logic. You must have a typo in there somewhere.

public static void main(String[] args) {

    Scanner myScan = new Scanner(System.in);
    String firstname;
    String middleinitial;

    System.out.println("What is your first name?");
    firstname = myScan.nextLine();

    System.out.println("What is your middle initial?");
    middleinitial = myScan.nextLine();

    String[] firstNameLetter = {"A", "Albert", "Alice", "Ann", "Anna", 
        "Annie","Arthur", "B", "Bernard", "Bette", "Bettie", "Betty", 
        "C", "Carl", "Catherine", "Charles", "Clara", "D", "Donald", 
        "Dorothy, E", "Edward", "Elizabeth", "F", "Florence", "Frank", 
        "G", "George", "Grace", "H", "Harold", "Harriet", "Harry", "Hazel",
        "Helen", "Henry", "I", "J", "James", "Jane", "Jayne", "Jean", 
        "John", "Joan", "Joseph", "K", "L", "M", "Margaret", "Martin",
        "Marvin", "Mary", "Melvin", "Mildred", "N", "O", "P",
        "Patricia", "Paul", "Q", "R", "Richard", "Ruby", "Robert",
        "Ruth", "S", "T", "Thelma", "Thomas", "U", "V",
        "W", "Walter", "Wanda", "William", "Wilma", "X", "Y",
        "Z"};

    int[] firstNameNumber = {000, 020, 020, 040, 040, 040, 040,
                             060, 80, 80, 80, 80, 100, 120,120,
                             140, 140, 160, 180, 180, 200, 220, 
                             220, 240, 260, 260, 280, 300, 300,
                             320, 340, 340, 360, 360, 380, 380,
                             400, 420, 440, 440, 440, 460, 460,
                             480, 480, 500, 520, 540, 560, 560,
                             580, 580, 600, 600, 620, 640, 660,
                             680, 680, 700, 720, 740, 740, 760,
                             760, 780, 800, 820, 820, 840, 860,
                             880, 900, 900, 920, 920, 940, 960, 980};  

    String[] middleNameLetter = {" ", "A", "B", "C", "D", "E", "F",
                                 "G", "H", "I", "J", "K", "L", "M",
                                 "N", "O", "P", "Q", "R", "S", "T",
                                 "U", "V", "W", "X", "Y", "Z"};

    int[] middleNameNumber = {0, 1, 2, 3, 4, 5, 6,
                              7, 8, 9, 10, 11, 12, 13,
                              14, 14, 15, 15, 16, 17, 18,
                              18, 18, 19, 19, 19, 19};

    for (int count = 0; count < firstNameLetter.length; count++) {
        if (firstname.equals(firstNameLetter[count])) {
            System.out.println(firstNameNumber[count]);
        }
    }

    for (int count = 0; count < middleNameLetter.length; count++) {
        if (middleinitial.equals(middleNameLetter[count])) {
            System.out.println(middleNameNumber[count]);
        } 
    }
}

Upvotes: 1

You can turn the array into a list and use the method. list.contains() Arrays.asList(firstNameLetter).contains(firstname);

etc etc for all the given inputs

Upvotes: 0

Ryan Waskiewicz
Ryan Waskiewicz

Reputation: 321

As a sanity check, are you able to recompile this successfully? In trying to run your solution I found the Scanner instance was not being declared. Adding the following: Scanner myScan = new Scanner(System.in);

allowed me to run with following output:

$java ScannerTest
What is your first name?
Henry
What is your middle initial?
A
380
1

Upvotes: 0

Saif Charaniya
Saif Charaniya

Reputation: 131

You might want to try equalsIgnoreCase() instead of equals() and may also want to add a break statement in the for loops to stop iterating if the strings match.

Upvotes: 0

Related Questions