1

Here's the code -

import java.util.Scanner;
public class assn9 {


    public static void main(String[] args){
        String[][] stateCapital = {
                { "Alabama", "Montgomery" },
                { "Alaska", "Juneau" },
                { "Arizona", "Phoenix" },
                { "Arkansas", "Little Rock" },
                { "California", "Sacramento" },
                { "Colorado", "Denver" },
                { "Connecticut", "Hartford" },
                { "Delaware", "Dover" },
                { "Florida", "Tallahassee" },
                { "Georgia", "Atlanta" },
                { "Hawaii", "Honolulu" },
                { "Idaho", "Boise" },
                { "Illinois", "Springfield" },
                { "Indiana", "Indianapolis" },
                { "Iowa", "Des Moines" },
                { "Kansas", "Topeka" },
                { "Kentucky", "Frankfort" },
                { "Louisiana", "Baton Rouge" },
                { "Maine", "Augusta" },
                { "Maryland", "Annapolis" },
                { "Massachusettes", "Boston" },
                { "Michigan", "Lansing" },
                { "Minnesota", "Saint Paul" },
                { "Mississippi", "Jackson" },
                { "Missouri", "Jefferson City" },
                { "Montana", "Helena" },
                { "Nebraska", "Lincoln" },
                { "Nevada", "Carson City" },
                { "New Hampshire", "Concord" },
                { "New Jersey", "Trenton" },
                { "New York", "Albany" },
                { "New Mexico", "Santa Fe" },
                { "North Carolina", "Raleigh" },
                { "North Dakota", "Bismark" },
                { "Ohio", "Columbus" },
                { "Oklahoma", "Oklahoma City" },
                { "Oregon", "Salem" },
                { "Pennslyvania", "Harrisburg" },
                { "Rhode Island", "Providence" },
                { "South Carolina", "Columbia" },
                { "South Dakota", "Pierre" },
                { "Tennessee", "Nashville" },
                { "Texas", "Austin" },
                { "Utah", "Salt Lake City" },
                { "Vermont", "Montpelier" },
                { "Virginia", "Richmond" },
                { "Washington", "Olympia" },
                { "West Virginia", "Charleston" },
                { "Wisconsin", "Madison" },
                { "Wyoming", "Cheyenne" } };

                int correctCount = 0;

                for (int i = 0; i < stateCapital.length; i++)
                {
                System.out.println("What is the capital of " + stateCapital[i][0] + "?");
                Scanner input = new Scanner(System.in);
                String capital = input.next();


                if (capital.equalsIgnoreCase(stateCapital[i][1])) {
                    correctCount++;
                    System.out.println("Your answer is correct, the correct count is " + correctCount);

                }
                else {

                    System.out.println("The correct answer should be " + stateCapital[i][1] + " and the correct count is " + correctCount);
                }
                }

                }
                }

So, instead of having the console ask what each capital is in the order that I typed them in the string, I want to randomize the order they are asked in and I want to limit each run to five questions. I'm kinda lost on this one. Thanks.

2
  • Do want to chose each one exactly once, but in random order? Commented Nov 8, 2013 at 2:59
  • I want to choose five at random. Commented Nov 8, 2013 at 3:16

4 Answers 4

1

You can declare a List for storing index of stateCapital. And call Collections.shuffle method to make the indexList randomly.

Then you can loop the indexList to show the questions. This is very simple. You just make the following 2 tiny changes.

  1. Add the following code: before looping the questions.

    List<Integer> indexList = new ArrayList<Integer>();
    for(int idx =0; idx <  stateCapital.length; idx++)
    {
        indexList.add(idx);
    }
    Collections.shuffle(indexList);
    
  2. Make some change for for-loop.

From

 for (int i = 0; i < stateCapital.length; i++)

to

 for(int i : indexList)

if You just need 5 questions, then you can use following code

 for(int i : indexList.subList(0, 5))

Then all of the questions will displayes randomly and there is no need to change other code.

Completely code is as follows:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class assn9 {

public static void main(String[] args) {
    String[][] stateCapital = { { "Alabama", "Montgomery" },
            { "Alaska", "Juneau" }, { "Arizona", "Phoenix" },
            { "Arkansas", "Little Rock" }, { "California", "Sacramento" },
            { "Colorado", "Denver" }, { "Connecticut", "Hartford" },
            { "Delaware", "Dover" }, { "Florida", "Tallahassee" },
            { "Georgia", "Atlanta" }, { "Hawaii", "Honolulu" },
            { "Idaho", "Boise" }, { "Illinois", "Springfield" },
            { "Indiana", "Indianapolis" }, { "Iowa", "Des Moines" },
            { "Kansas", "Topeka" }, { "Kentucky", "Frankfort" },
            { "Louisiana", "Baton Rouge" }, { "Maine", "Augusta" },
            { "Maryland", "Annapolis" }, { "Massachusettes", "Boston" },
            { "Michigan", "Lansing" }, { "Minnesota", "Saint Paul" },
            { "Mississippi", "Jackson" }, { "Missouri", "Jefferson City" },
            { "Montana", "Helena" }, { "Nebraska", "Lincoln" },
            { "Nevada", "Carson City" }, { "New Hampshire", "Concord" },
            { "New Jersey", "Trenton" }, { "New York", "Albany" },
            { "New Mexico", "Santa Fe" }, { "North Carolina", "Raleigh" },
            { "North Dakota", "Bismark" }, { "Ohio", "Columbus" },
            { "Oklahoma", "Oklahoma City" }, { "Oregon", "Salem" },
            { "Pennslyvania", "Harrisburg" },
            { "Rhode Island", "Providence" },
            { "South Carolina", "Columbia" }, { "South Dakota", "Pierre" },
            { "Tennessee", "Nashville" }, { "Texas", "Austin" },
            { "Utah", "Salt Lake City" }, { "Vermont", "Montpelier" },
            { "Virginia", "Richmond" }, { "Washington", "Olympia" },
            { "West Virginia", "Charleston" }, { "Wisconsin", "Madison" },
            { "Wyoming", "Cheyenne" } };


    List<Integer> indexList = new ArrayList<Integer>();
    for(int idx =0; idx <  stateCapital.length; idx++)
    {
        indexList.add(idx);
    }
    Collections.shuffle(indexList);

    int correctCount = 0;

    //for (int i = 0; i < indexList.size(); i++) {
    for(int i : indexList){
        System.out.println("What is the capital of " + stateCapital[i][0]
                + "?");
        Scanner input = new Scanner(System.in);
        String capital = input.next();

        if (capital.equalsIgnoreCase(stateCapital[i][1])) {
            correctCount++;
            System.out
                    .println("Your answer is correct, the correct count is "
                            + correctCount);

        } else {

            System.out.println("The correct answer should be "
                    + stateCapital[i][1] + " and the correct count is "
                    + correctCount);
        }
    }

}
}
Sign up to request clarification or add additional context in comments.

Comments

0
Random r = new Random();
Scanner s = new Scanner(System.in);

for (int i = 0; i < 5; i++) {
    int random = r.nextInt(50);
    System.out.printf("What is the capital of %s?\n", stateCapital[random][0]);
    String input = s.next();
    if (input.equalsIgnoreCase(stateCapital[random][1])) {
        // Handle correct answer
    } else {
        // Handle incorrect answer
    }
}

1 Comment

It won't randomize the correct answers, since it is accessing the same array for the question and the answer. The value in random won't change between the output and the if statement.
0

I would be lazy and create a function which returns x number of unique random values from within a range:

private static final int[] randomIndices(int datasize. int howmany) {
    if (howmany > datasie) {
        throw new IllegalArumentException();
    }
    int[] indices = new int[howmany];
    for (int i = 0; i < howmany; i++) {
        boolean ok = true;
        do {
            ok = true;
            indices[i] = (int)(datasize * Math.random());
            for (int j = 0; ok && j < i; j++) {
                if (indices[j] == indices[i]) {
                    ok = false;
                }
            }
        } while (!ok);
    }
    return indices;
}

Then, in your main method, I would do:

int[] toask = randomIndices(statecapitals.length, 5);
for (int i = 0; i < toask.length; i++) {
     int question = toask[i];
     ......
     // ask question, etc.
}

Comments

0

To pick 5 random indexes:

List<Integer> indexes = new ArrayList<Integer>(); // create a list to hold all indexes
for (int i = 0; i < stateCapital.length; i++) // populate the list with all indexes
    indexes.add(i);
Collections.shuffle(indexes); // use API to randomly order (shuffle) them
for (Integer i : indexes.subList(0, 5)) { // loop over the first 5
    // your current loop body here
}

Comments

Your Answer

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.