Results 1 to 7 of 7
  1. #1
    Bedwards6968 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default JOptionPane and Arrays

    I'm trying to figure out how to use a JOptionPane to search an Array for an answer to a input field?

    In this program I have a double array with states and their capitals in one class. In another class I have the main method where I will use a JOptionPane to randomly ask the user to enter a Capital city of a "random" state. If the user answers correctly another Pane will pop up with the saying "your answer is correct!".

    Here is what I have so far...

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Lab5 {
    
    	//Main Method
    	public static void main(String[] args) {
                
            StateCapitals statecapitals1 = new StateCapitals();
                
            statecapitals1.GetCapital();
            
            JOptionPane.showInputDialog("What is the capital of " + statecapitals1.GetCapital());
            
            JOptionPane.showMessageDialog(null,"Your answer is correct!");
     
    
    	}
    }
    Java Code:
    class StateCapitals {   
      
        private 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"},   
            {"Maryland", "Annapolis"}, {"Minnesota", "Saint Paul"}, {"Iowa",   
             "Des Moines"}, {"Maine", "Augusta"}, {"Kentucky", "Frankfort"}, {"Indiana",   
            "Indianapolis"}, {"Kansas", "Topeka"}, {"Louisiana", "Baton Rouge"},   
            {"Oregon", "Salem"}, {"Oklahoma", "Oklahoma City"}, {"Ohio", "Columbus"},   
            {"North Dakota", "Bismark"}, {"New York", "Albany"}, {"New Mexico",   
            "Santa Fe"}, {"New Jersey", "Trenton"}, {"New Hampshire", "Concord"},   
            {"Nevada", "Carson City"}, {"Nebraska", "Lincoln"}, {"Montana", "Helena"},   
            {"Missouri", "Jefferson City"}, {"Mississippi", "Jackson"},   
            {"Massachusettes", "Boston"}, {"Michigan", "Lansing"}, {"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"}   
            };   
      
        protected String [][] GetCapital()   
        {   
            return stateCapital;   
        }   
      }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JOptionPane and Arrays

    Quote Originally Posted by Bedwards6968 View Post
    I'm trying to figure out how to use a JOptionPane to search an Array for an answer to a input field?
    Actually the JOptionPane can't do any searching for you. You can only use it to get or display information to the user.


    In this program I have a double array...
    A minor nitpick: you actually have a 2-dimensional array of String. A double array looks like this: double[].


    Here is what I have so far...

    Java Code:
    public class Lab5 {
    
    	//Main Method
    	public static void main(String[] args) {
                
            StateCapitals statecapitals1 = new StateCapitals();
    
            // (A)            
            statecapitals1.GetCapital(); 
    
            // (B)
            JOptionPane.showInputDialog("What is the capital of " + statecapitals1.GetCapital()); 
            
            JOptionPane.showMessageDialog(null,"Your answer is correct!");
     
    
    	}
    }
    The code line below // (A) doesn't make sense as it doesn't do anything.
    The code line below // (B) also doesn't make sense either. Don't you want to declare a String variable here and capture the reply the user makes? Why are you passing a 2-dimensional array into the showInputDialog?


    Java Code:
    class StateCapitals {   
      
        private 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"},   
            {"Maryland", "Annapolis"}, {"Minnesota", "Saint Paul"}, {"Iowa",   
             "Des Moines"}, {"Maine", "Augusta"}, {"Kentucky", "Frankfort"}, {"Indiana",   
            "Indianapolis"}, {"Kansas", "Topeka"}, {"Louisiana", "Baton Rouge"},   
            {"Oregon", "Salem"}, {"Oklahoma", "Oklahoma City"}, {"Ohio", "Columbus"},   
            {"North Dakota", "Bismark"}, {"New York", "Albany"}, {"New Mexico",   
            "Santa Fe"}, {"New Jersey", "Trenton"}, {"New Hampshire", "Concord"},   
            {"Nevada", "Carson City"}, {"Nebraska", "Lincoln"}, {"Montana", "Helena"},   
            {"Missouri", "Jefferson City"}, {"Mississippi", "Jackson"},   
            {"Massachusettes", "Boston"}, {"Michigan", "Lansing"}, {"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"}   
            };   
    
        // (C)
        protected String [][] GetCapital()   
        {   
            return stateCapital;   
        }   
      }
    The method below // (C) concerns me, and I fear that it is incorrect. First for a minor nitpick, method names should begin with a lower case letter, not a capital letter. But more importantly, this method doesn't get any State capital. All it does is return the entire array declared in the class which doesn't seem that helpful for your goals to me. Perhaps you will want to post your assignment instructions to see what this method should return, or to see if you are in fact correct?

    Regarding your main problem, you will want to use JOptionPane.showInputDialog(...) get the input from the user, then your main method will need to use the String returned in some code that you write to see if the String is a valid state capital. We'll work on that once we know more about your assignment instructions.

  3. #3
    Bedwards6968 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: JOptionPane and Arrays

    These are my entire instructions....

    1. Perform Programming exercise on 9.22.
    2. You are to randomly select 10 out of the 50 states to be asked from the StateCapitals.java program.
    3. You are not required to sort the data when reading it back into your program for display.

    Exercise 9.22

    1. Write a program that repeatedly prompts the user to enter a capital for a state.
    2. Upon receiving the user input, the program reports whether the answer is correct.
    3. Assume that 50 states and their capitals are stored in a two-dimensional array.
    4. The program prompts the user to answer all ten states' capitals and display the total correct count.

  4. #4
    Bedwards6968 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: JOptionPane and Arrays

    The StateCapitals.java was a program that the teacher created and insturcted us to use.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JOptionPane and Arrays

    Quote Originally Posted by Bedwards6968 View Post
    These are my entire instructions....

    1. Perform Programming exercise on 9.22.
    2. You are to randomly select 10 out of the 50 states to be asked from the StateCapitals.java program.
    3. You are not required to sort the data when reading it back into your program for display.

    Exercise 9.22

    1. Write a program that repeatedly prompts the user to enter a capital for a state.
    2. Upon receiving the user input, the program reports whether the answer is correct.
    3. Assume that 50 states and their capitals are stored in a two-dimensional array.
    4. The program prompts the user to answer all ten states' capitals and display the total correct count.
    OK, this is how I interpret the instructions:

    1. Write a program that repeatedly prompts the user to enter a capital for a state.

    1. Use a Random object to randomly select a state/capital pair from the 2-dimensional array.
    2. The Random class has a method, nextInt(int max) that can be used to select an int from 0 to 50 - 1.
    3. Store the state and capital Strings in two variables.
    4. Use your JOptionPane.showInputDialog and a String that contains the state String obtained above to prompt the user for a capital, and store the input returned in another String variable, say called userInput.
    5. The "repeatedly" statement above means that you'll do the above in some type of loop, and you'll need to choose what type of loop would be best for this situation.


    2. Upon receiving the user input, the program reports whether the answer is correct.
    1. You'll want to compare the String that holds the state capital obtained from the 2-D array with the String entered by the user.
    2. Remember not to use == to compare Strings but rather one of the two String equals methods: equals(...) or equalsIgnoreCase(...).


    3. Assume that 50 states and their capitals are stored in a two-dimensional array.
    1. You'll want to create a 2-D array of String, and initialize it with the array returned from the GetCapital() method in your StateCapitals class.


    4. The program prompts the user to answer all ten states' capitals and display the total correct count.
    1. You'll need an int variable, say called correctCount, that is declared before your loop, and that counts the number of correct answers.


    Quote Originally Posted by Bedwards6968 View Post
    The StateCapitals.java was a program that the teacher created and insturcted us to use.
    And this code was copied directly from your teacher? Capitalization and all? Hmmmm...
    Last edited by Fubarable; 03-24-2012 at 06:36 PM.

  6. #6
    Bedwards6968 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: JOptionPane and Arrays

    Yes code was copied straight from the teacher, I thought it was strange as well because even I know about the capitalizations. But people make mistakes

  7. #7
    Bedwards6968 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: JOptionPane and Arrays

    ok Im understanding what you are saying how to do the program but assigning the strings to the arrays is where Im getting confused at.

Similar Threads

  1. NEED HELP!!! JOptionPane and arrays
    By Corrrrr in forum New To Java
    Replies: 10
    Last Post: 03-13-2012, 09:23 AM
  2. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  3. JOptionPane Help
    By adjit in forum New To Java
    Replies: 23
    Last Post: 01-04-2012, 03:20 PM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  5. JOptionPane
    By Mir in forum New To Java
    Replies: 5
    Last Post: 11-29-2008, 03:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •