Fstagger
Fstagger

Reputation: 55

Compare the index of 2 String arrays with different values Java

I have 2 parallel arrays: the first contains State Names, the second Capitals of the states.

I'm making a quiz that randomly generates a State then asks the user to enter the Capital of the state. Once the input is received I want to call a method to check if the index of the capital entered is the same as the index of the state it goes with.

ie: stateArray[0] = "New York" and capitalArray[0] = "Albany".

Check Answer Method

 public static void checkAnswer(String[]stateArray, String capitalArray, String answer)
 {
    int index;
    for (int i = 0; i < capitalArray.length; i++){

        if(capitalArray[i].equalsIgnoreCase(answer)){
            index = i;
        }
    }

    if(capitalArray[index] == stateArray[index]) 
        {
            System.out.println("correct");
        }
        else
        {
            System.out.println("incorrect");
        }
    }

I know the second if statement is wrong. How can I compare the two arrays using the index where the users answer was found in the capitalArray?

Upvotes: 0

Views: 5484

Answers (5)

duggu
duggu

Reputation: 38439

Try this function it return array:-

public static String[] numSame (String[] list1, String[] list2) 
     {  
          int same = 0;  
          for (int i = 0; i <= list1.length-1; i++) 
          {  
             for(int j = 0; j <= list2.length-1; j++) 
             {  
                if (list1[i].equals(list2[j])) 
                {  
                    same++;  
                    break;  
                }  
             }  
          }  

          String [] array=new String[same];
          int p=0;
          for (int i = 0; i <= list1.length-1; i++) 
          {  
             for(int j = 0; j <= list2.length-1; j++) 
             {  
                if (list1[i].equals(list2[j])) 
                {  
                    array[p]=  list1[i]+"";
                    System.out.println("array[p] => "+array[p]);
                    p++;
                    break;  
                }  
             }  
          } 
          return array;
       }  

Upvotes: 0

Cris Holdorph
Cris Holdorph

Reputation: 319

An alternate implementation in Java would be to use a Map instead of two arrays.

Map<String,String> stateCapitals = new HashMap<String,String>();
stateCaptitals.put("New York", "Albany");

then you can check the map with

public voic checkAnswer(String chosenState, String chosenCapital) {
  if (stateCapitals.get(chosenState).equals(chosenCapital) {
     System.out.println("you are correct!");
  }
}

This does not do it with 2 parallel arrays, but it is a better implementation if your real concern is the type of data you mentioned, and not the arrays themselves.

Upvotes: 0

PM 77-1
PM 77-1

Reputation: 13334

Since you know what state you asked about you should know its array index as well. As you see below both arrays are declared as class variables.

... class Quiz {

private String[] states = new String[50];
private String[] capitals = new String[50];

... method to fill both arrays with the correct data

 public static void checkAnswer(int question, String answer)
 {


    if(capitalArray[question].equalsIgnoreCase(answer)){
        {
            System.out.println("correct");
        }
        else
        {
            System.out.println("incorrect");
        }
   }
}

It's better to have checkAnswer method's return type as Boolean, but I left it your way.

Upvotes: 0

LuiNova
LuiNova

Reputation: 396

You need to keep track of the index that holds the State displayed to the user. For example, the way your code is written now gives the user the ability to get a right answer by giving a wrong answer. Take this example as explanation:

string[] stateArray = {"New York", "California"};
string[] capitalArray = {"Albany", "Sacramento"};

If you were to show "New York" as the question and the user happens to answer "Sacramento" your code would display correct.

You also need a case in which the answer does not match any of the capitals in the array. One way of doing this to implement in your code is to initiate the index to -1.

int index = -1;

Once you finish the for loop check if index is -1 and display "Your answers is not a valid State" or something along those lines.

Maybe use a HashMap, I am not completely familiar with Java it appears to be the similar to a Dictionary in Python. Dictionary object has great performance.

Upvotes: 0

Joe Elleson
Joe Elleson

Reputation: 1373

boolean checkAnswer(String[] stateArray, String[] capitalArray, String displayedState, String answer) {
    for (int i = 0; i < stateArray.length; i++) {
        if (stateArray[i].equals(displayedState) && capitalArray[i].equals(answer)) {
            return true;
        }
    }
    return false;
}

Or something. The key is you need to pass in something to represent the state you displayed.

Upvotes: 1

Related Questions