Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JOptionPane and Arrays

 
Brad Edwards
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...



 
Jhon Milton
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to get the input from JOptionPane input dialog and assign it to a String.

String userEnteredCapital= JOptionPane.../ your input dialog

// do some logic to verify this value

if(true)
//display JOptionPane with correct message

else
//display JOptionPane with wrong message

Thanks
Jhon Milton
 
Mohamed Sanaulla
Bartender
Posts: 3166
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read this tutorial? It shows how to take input from the JOptionPane component.
 
Mohamed Sanaulla
Bartender
Posts: 3166
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brad Edwards wrote:I'm trying to figure out how to use a JOptionPane to search an Array for an answer to a input field?

Once you have figured out how to capture the input value from the JOptionPane then searching for the answer in an array should be trivial.
 
Brad Edwards
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Ive read the tutorial but still didnt completely grasp the concept of it. Mainly not know what to put where. I already figured out that I need to do some if statements in order to show whether or not the answer was correct.
 
Mohamed Sanaulla
Bartender
Posts: 3166
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brad Edwards wrote:Yes Ive read the tutorial but still didnt completely grasp the concept of it. Mainly not know what to put where. I already figured out that I need to do some if statements in order to show whether or not the answer was correct.

But before that you need to get the value entered in the JOptionPane. The tutorial says how to do that- in short you need to capture the return value of the showInputDialog method and then use it to search in the Array. You have a 2 dimensional array so each element of the array is an array of Capital and States. Your task (once you have obtained the value entered in the dialog) is to iterate through the array and then check if the value entered if present in the array.

So the steps would be:
Assign the return value of dialog to a variable v.
Iterate through the [capital,state] array to verify if the value in 'v' is present.
 
Brad Edwards
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamed Sanaulla
Bartender
Posts: 3166
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brad Edwards wrote:

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.


From what I have understood you would have to:
1. pick 10 states from the original array.
2. for each state from the array selected in step-1 ask for user input and store it in a variable (read here on how to do it)
3. check if the user entered capital is equal to the one in the array, if yes then increment the correct count.
4. go to step 2 until you have accepted input for 10 elements.

there are a few open issues here though like:
1. how will you pick 10 elements from the original 2D array?

 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!