archer
archer

Reputation: 89

Need some help retrieving data from a parallel array / output statement

package statescapitalquizz;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
*
* @author Steve
*/
public class Statescapitalquizz {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String fileName = "Capitals.txt";
        boolean found = false;
        Scanner kb = new Scanner(System.in); 

        String target; 
        int n = 50;

        String[] states = new String[n];
        String[] capitals  = new String[n];

        try (Scanner inputStream = new Scanner(new FileInputStream(fileName))) {

            for (int i = 0; i < n; i++) {
                states[i] = inputStream.nextLine();
                capitals[i]   = inputStream.nextLine();
            }

        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }


        System.out.println("Please enter the name of a state: "); 
        target = kb.nextLine();


        // the loop coninues to the end of the array if the city is not found  
        for (n=0; (!found) && (n < states.length) ; n++)  {
            if (states[n].matches(target)) {
                //print found message and set found to true
                System.out.println("The capital of " + target + " is"); //Where I need help
                found = true;
            } // end if

        } // for loop


        // after the loop – if not(found) print not found message
        if (!found)
            System.out.println(target + "is not a state in the United States");

        }
    }

Okay so the part I need help is how to retrieve the matching 'capital' to the 'state' that was entered by the user. My output statement is basically

//print found message and set found to true
      System.out.println("The capital of " + target + " is" + ); //Where I need help
      found = true;
  } // end if

 } // for loop

And I don't know what code to put in the output statement so it cross references my second array and puts the matching Capital from the State the user entered.

Upvotes: 0

Views: 64

Answers (1)

archer
archer

Reputation: 89

System.out.println("The capital of " + target + " is " + capitals[n]);

As mentioned in the comments to my question, is the answer.

Upvotes: 1

Related Questions