0

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?

Luiggi Mendoza
85.9k16 gold badges159 silver badges353 bronze badges
asked Apr 5, 2013 at 20:56
6
  • it looks like you need to compare the index of the answer that was given to the index of the state that you displayed Commented Apr 5, 2013 at 21:00
  • And treat the case when the answer does not match any String in capitalArray. Commented Apr 5, 2013 at 21:00
  • You don't seem to have anything to represent the correct state, so you won't be able to find the appropriate index to lookup Commented Apr 5, 2013 at 21:07
  • Please show us the code that asks a question and captures the answer. Commented Apr 5, 2013 at 21:12
  • You'll need to store the question before 'asking' it, or better yet, store the index of the question. Then you'll be able to use this to check the answer if(capitalArray[questionIndex].equals(answer)) return true;. Note, the second parameter should be String[] instead of String, but I guess that's a typo in your example. Commented Apr 5, 2013 at 21:16

5 Answers 5

1
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.

answered Apr 5, 2013 at 21:16
0

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.

answered Apr 5, 2013 at 21:22
0

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.

answered Apr 5, 2013 at 21:28
0

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.

answered Apr 5, 2013 at 21:49
0

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;
 } 
answered Aug 28, 2013 at 6:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.