Home > Net >  2D array for state and capitols, with system asking user input, and then validating answer
2D array for state and capitols, with system asking user input, and then validating answer

Time:11-08

I am in college and I have this assigment for data structures and algorithms, this is the project: "Develop a program that asks the user to enter a capital for a U.S. state. Upon receiving the user input, the program reports whether the user input is correct. For this application, the 50 states and their capitals are stored in a two-dimensional array in order by state name. Display the current contents of the array then use a bubble sort to sort the content by capital. Next, prompt the user to enter answers for all the state capitals and then display the total correct count. The user's answer is not case-sensitive."

This is what I have done so far

import java.util.Scanner;
import java.util.Arrays;
import java.lang.String;
public class Assignment {
// It begins with the creation of the two dimension array that includes state and capital.
    public static void main (String[] args) {
        String[][] StateAndCapital = {
                {"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"},
                {"Massachusetts", "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 Mexico", "Santa Fe"},
                {"New York", "Albany"},
                {"North Carolina", "Raleigh"},
                {"North Dakota", "Bismarck"},
                {"Ohio", "Columbus"},
                {"Oklahoma", "Oklahoma City"},
                {"Oregon", "Salem"},
                {"Pennsylvania", "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"}
        };

So far I have created the 2d array, I need to have a for loop so the system goes through the array. But I am not sure on how to create a for loop for a two dimension array, I have watched videos for bubble sorting (required for the assignment) and what my lessons mention but they mainly show how to do it on a single dimension array.

  for (int i = 1; i < array.length; i  ) {
    for (int j = i; j > 0; j--) {
     if (array[j] < array [j - 1]) {
      temp = array[j];
      array[j] = array[j - 1];
      array[j - 1] = temp;

That is from a lesson, using a single array, but not sure how to do it with a two dimension array.

I also looked into how to convert string to boolean so whenever the user answered, the system would say either "correct answer" or "incorrect answer". If I could get some advice please, I am starting and this may be a dumb question.

I tried changing string value to boolean values so whenever I replied to the system I would get a "correct" or "wrong" answer according to the true/false values assigned to an array of only states, but all answers seemed to say true regardless of my input, which is odd. I am still looking for a way to validate string values. I have used the scanner to scan the answer given by the user, looked at boolean programs to see if I can see something that I could use but try to make it string-based.

CodePudding user response:

You sort the "2D" array just like a "1D" array. You have 50 rows and 2 columns. Assuming you know how to sort a "1D" array and using i and j you would:

  • compare arr[i][1] against a[j][1] to sort by capital
  • then, when time swap arr[i][1] and arr[j][1] and also swap arr[i][0] and arr[j][0]. (or just like your example sort, but you need to swap both columns to keep each state and its capital together).

For the quiz, if a state is presented asking for its capital, just prompt for the capital and compare to the correct answer. Similarly for presenting a capital and asking for state. I recommend you compare everything using lower case to avoid problems in comparing.

Check out the String class for information on using compareTo and toLowerCase or toUpperCase.

CodePudding user response:

(1) For a 2D array, an expression such as array[j] references an entire row of array, i.e. array[j][0], array[j][1], array[j][2], array[j][3], ...

To access a single element, you need to specify each subscript.

  System.out.println ("The capital of "   stateAndCapital[i][0]
        " is "   stateAndCapital[i][1]   ".");      

(2) Don't use <, <=, >, >=, ==, != on reference types. String is a reference type. The String API offers some methods for comparison:

  • .equals (String other)
  • .equalsIgnoreCase (String other)
  • .compareTo (String other)
  • .compareToIgnoreCase (String other)

(3) You want to sort by column 1. That is, compare one stateAndCapital[x][1] to another stateAndCapital[y][1]. (Remember (1): Specify both subscripts)

When you need to swap, you need to swap both column 1 and column 0 for the indexed row. One way to do that is to make temp a 1D array of String:

   String[] temp;  
   ... 
   temp = stateAndCapital[j];
   stateAndCapital[j] = stateAndCapital[j - 1];
   stateAndCapital[j - 1] = temp;

That works because of the first part of (1): Using a single subscript on a 2D array references an entire row.

At least for testing purposes do these:

  • Fix the bubble sort using the 3 issues above.
  • Print the the state names and capitals from the original array.
  • Do the bubble sort.
  • Print the state names and capitals again.

"all answers seemed to say true regardless of my input, which is odd"

We can't really help with that issue, because you didn't provide example code. But, dealing with that now risks getting the question closed for not being focused. My suggestion is to fix and test the bubble sort first. Keep the String comparison issue in mind for the rest of your code. If you get stuck again, ask a new question. Reference this question in that, and add a comment to this question to point to the new question.

Note: I used stateAndCapital instead of StateAndCapital to conform to Java naming convention.

  • Related