Results 1 to 7 of 7
Thread: JOptionPane and Arrays
- 03-24-2012, 04:38 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
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; } }
-
Re: JOptionPane and Arrays
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...
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 // (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; } }
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.
- 03-24-2012, 05:20 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
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.
- 03-24-2012, 05:21 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Re: JOptionPane and Arrays
The StateCapitals.java was a program that the teacher created and insturcted us to use.
-
Re: JOptionPane and Arrays
OK, this is how I interpret the instructions:
1. Write a program that repeatedly prompts the user to enter a capital for a state.
- Use a Random object to randomly select a state/capital pair from the 2-dimensional array.
- The Random class has a method, nextInt(int max) that can be used to select an int from 0 to 50 - 1.
- Store the state and capital Strings in two variables.
- 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.
- 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.
- 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.
- 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.
- 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.
- You'll need an int variable, say called correctCount, that is declared before your loop, and that counts the number of correct answers.
And this code was copied directly from your teacher? Capitalization and all? Hmmmm...Last edited by Fubarable; 03-24-2012 at 05:36 PM.
- 03-24-2012, 05:48 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
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
- 03-24-2012, 06:07 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
NEED HELP!!! JOptionPane and arrays
By Corrrrr in forum New To JavaReplies: 10Last Post: 03-13-2012, 08:23 AM -
Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02-17-2012, 12:49 PM -
JOptionPane Help
By adjit in forum New To JavaReplies: 23Last Post: 01-04-2012, 02:20 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
JOptionPane
By Mir in forum New To JavaReplies: 5Last Post: 11-29-2008, 02:04 AM
Bookmarks