I'm trying to get the index of what the user chooses so that I can call the class object with the number from the drop-down menu.
String[] storeListArr;
Object storePick;
ArrayList<String> storeList = new ArrayList<>();
while (!(newStore[nCount] == null)) {
storeList.add(newStore[nCount].getName());
nCount++;
} //end loop
storeListArr = new String[storeList.size()];
storePick = JOptionPane.showInputDialog(null, "Choose Store", "Store Selection", JOptionPane.QUESTION_MESSAGE, null, storeListArr, storeListArr[0]);
So if the user picks newStore1 from the drop-down menu, I can call the store class and get whatever I want from it.
I'm trying to get the index of what the user chooses
The showInputDialog(...) only returns the String that was selected.
If you want to know the index then you need to use methods of the ArrayList:
storePick = JOptionPane.showInputDialog(...);
int index = storeList.indexOf( storePick );
Related
Basically i want to enter the names as long as i don't cancel the InputMessageDialog, then i want to asign my names to variable created before and print them out at the end in the MessageDialog. I was trying some stuff outside the loop but got the notificacion that "value 'names' is always 'null'"
String names;
while (true) {
names = JOptionPane.showInputDialog(null, "ENTER THE NAMES");
if (names == null) {
JOptionPane.showMessageDialog(null, "ENTRY CANCELED!");
break;
} else {
}
}
JOptionPane.showMessageDialog(null, "YOUR NAMES: " + names);
Your code is pretty close to what you describe as your goal – the primary thing missing is that you need to keep track of the various values along the way, and print them at the end.
The code you posted will loop again and again asking for a single value (which you are storing into String names - a little confusing choice for variable name, since it contains only one name input). As you found, when the user hits the cancel button (to end the loop), it sets names to null. The final step shows a dialog box with the last value for names (which is always null).
Here's a program that:
loops until the user hits the "cancel" button (which would set input to be null), or if they enter a blank value – this allows the user to exit by simply hitting return without typing anything
adds all non-empty input values to a java.util.Set – this is an arbitrary choice, use whatever data structure is appropriate for your program
shows a final dialog with the contents of the set
import javax.swing.*;
import java.util.HashSet;
import java.util.Set;
public class t {
public static void main(String[] args) {
Set<String> values = new HashSet<>();
while (true) {
String input = JOptionPane.showInputDialog(null, "enter something");
if (input != null && !input.isEmpty()) {
values.add(input);
} else {
break;
}
}
JOptionPane.showMessageDialog(null, "all values: " + values);
}
}
If I run with the following input:
one
two two
three three three
<blank>
Then the final dialog message is:
all values: [one, two two, three three three]
Note that a java.util.Set doesn't necessarily return items in any specific order, it just happens to have worked out that way in this example.
String[] enrollment = {"first Value", "second value"}
int enroll = JOptionPane.showOptionDialog(null,
"Please select your enrollment:", "Enrollment",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
enrollment, enrollment[1]);
How can I get the value from enroll (like first Value) and compare it in a if statement? Since enroll returns an integer variable.
I dont think this question is a duplicate as marked because I am just trying to get the value inside the integer, store it and compare later.
You may have to do something like this in your if/else checking
if (enroll != JOptionPane.CLOSED_OPTION) {
System.out.println(enrollment [enroll ]);
} else {
System.out.println("No option selected".);
}
if (enroll == 0) { // 0 here is the first thing in the enrolment array
// do something
}
I am using a JOptionPane with input dialog. I am having trouble catching the value of the choice so that I can use it later in my program.
String[] options = {"Selection Sort", "Insertion Sort"};
Object searchType = JOptionPane.showInputDialog(null, null, "Choose a sort type ",
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
…and this is what it looks like.
edit: I am having trouble catching the option that is chosen by the user.
I have tried:
int selection = JOptionPane.QUESTION_MESSAGE;
and that will compile, however I can't actually use the value.
basically when a person selects one of the two options I want to know which one it is.
edit2: For future reference this works:
Object selection = searchType;
if(selection.equals(options[0]))
{
//something
}
else if(selection.equals(options[1]))
{
//something else
}
I think you should read the JavaDocs a little close...
Returns:
user's input, or null meaning the user canceled the input
This means, if the use selected Okay, that it will return the item the user selected as listed by the options parameter. In your case this will be Selection Sort or Insertion Sort or null if they canceled the dialog
Updated with example
Using this and selecting [Okay] outputs Selection Sort
String[] options = {"Selection Sort", "Insertion Sort"};
Object searchType = JOptionPane.showInputDialog(null, null, "Choose a sort type ",
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
System.out.println(searchType);
Take a closer look at How to Make Dialogs for more details
You should check the object returned and compare it to the items held in your array.
Either that, or call toString() on it and use the String value returned to decide.
I have an ArrayList of String type which will change in size and I want to display the elements of the string in the drop down menu how do I get each element in to it when I dont know the number of elements in the ArrayList.
Eg ArrayList1 = 1st,2nd,3rd
ArrayList2 = 1st,2nd,3rd,4th,5th
Then I want to be able to do something when the option is chose but I dont know how I would do the if else statements.
Any help would be much appreciated.
Object [] menu3 = {"1. first choice", "2. 2nd", "3. 3rd",};
Object select = JOptionPane.showInputDialog(null, "Choose:", "Choose a Report", 3, null, menu3, menu3[0]);
if(select.equals(menu3[0])) //how would I do this with an unknown number of selections
If you are using an ArrayList:
ArrayList<type> arr = ....;
To loop through it use a for:
for (type element : arr)
...
Sloved it there. As it is of type array you can just add the arraylist to the array like so
Object[] menu3 = new Object[list.size()];
menu3 = list.toArray(menu3);
I am new in Java. I'm taking my first java class. I am trying to create a fixed size array ( for example: the array with a size is 10) and I use JOptionPane to let the user input data. My question is how can I let the user stop their input whenever they want. (For example: They just input 3 data instead of 10 and they want to finish it). This is my first post, I'm sorry if the format is not correct. Thank you guys.
import java.util.Arrays;
import javax.swing.JOptionPane;
public class TestArray {
/**
* #param args
*/
public static void main(String[] args) {
String[] lastName = new String[10];
for (int i = 0; i < lastName.length; i++)
{
lastName[i] = JOptionPane.showInputDialog(null,"Please Enter Tutor Last Names: " );
}
JOptionPane.showMessageDialog(null, lastName);
Just break out of the loop when you encounter a null value or empty String. Click cancel or enter an empty String to break from the loop.
for (int i = 0; i < lastName.length; i++) {
lastName[i] =
JOptionPane.showInputDialog(null, "Please Enter Tutor Last Names:");
if (lastName[i] == null || lastName[i].isEmpty()) {
break;
}
}
just put a check using certain variable and prompt user at end of every cycle to continue or not...if user wants to quit simply use break;
but it is good practice that to get the no of iteration prior to rotating loop...ask user that for how much no of time they want to execute the steps...store it in variable and use that as exit condition...