Using JOptionPane.showInputDialog to return an integer

I am currently using JOptionPane to return a string from the Object array i made with all possible choices.

privatestaticboolean determineService(){

JLabel label;

label=new JLabel("Please select which Service Model to use.",JLabel.CENTER);

Object[] possibilities ={"AddressCleanse","VIN","PostalCode"};

serviceModel = (String)JOptionPane.showInputDialog(

null,

label,

"Select Service",

JOptionPane.PLAIN_MESSAGE,null,

possibilities,

"AddressCleanse");

//If a string was returned, say so.

if ((serviceModel !=null) && (serviceModel.length() > 0)){

System.out.println("You've selected " + serviceModel +"!");

setVariables();

returntrue;

}else{

//If you're here, the return value was null/empty.

System.out.println("Null or Bad service selected!\n Exiting program.");

returnfalse;

}

I would like to instead return the response as an integr so I can then use a switch, rather than recursive if, else if logic.

Apparently java doesnt allow switches to use strings (like PHP does)

any ideas?

[1991 byte] By [Masterkeedua] at [2007-11-27 9:01:45]
# 1
Integer.parseInt(string);
blackmagea at 2007-7-12 21:31:40 > top of Java-index,Java Essentials,Java Programming...
# 2
...forgive me, but that won't work unless there is an integer withing the string correct?I need a way to return the index of the all text selection e.g.;0,1,2Message was edited by: Masterkeedu
Masterkeedua at 2007-7-12 21:31:40 > top of Java-index,Java Essentials,Java Programming...
# 3
I think u ll have to do it by hand.String class doesnt provide methods to ur problem However it has useful methodsEg: U can access each character testing if it is a letter when u find a number u stop and use substring to copy a range of characters..:)
charllescubaa at 2007-7-12 21:31:40 > top of Java-index,Java Essentials,Java Programming...
# 4
Swing related questions should be posted in the Swing forum.You could use the Arrays.binarySearch(...) method to search your array for the value returned from the option pane. It will return the index.
camickra at 2007-7-12 21:31:40 > top of Java-index,Java Essentials,Java Programming...
# 5

Just a question more than an answer: would using a hashmap ever work here? where the key is a string and the value is an (?) object that implements an interface that would allow you to call an appropriate method? Perhaps one could use the keySet() and toArray to build the Object array for the JOptionPane. I thought I read something about this somewhere, but am not sure....

petes1234a at 2007-7-12 21:31:40 > top of Java-index,Java Essentials,Java Programming...
# 6

int index = java.util.Arrays.asList(possibilities).indexOf(JOptionPane.showInputDialog(

null,label,"Select Service",JOptionPane.PLAIN_MESSAGE,null,possibilities,"AddressCleanse"));

then test index > -1 (-1 = cancel/esc/X)

Michael_Dunna at 2007-7-12 21:31:40 > top of Java-index,Java Essentials,Java Programming...
# 7
Michael Dunn: Thank you!That is exactly what I needed.I'll roll it into my project as soon as Im back to work on it.
Masterkeedua at 2007-7-12 21:31:40 > top of Java-index,Java Essentials,Java Programming...
# 8
I replaced the code and i am amazed at how easy a solution that was...Thanks again!I can also still get the text version of the service(to output to user) by combining 'possibilities' with 'index'great!
Masterkeedua at 2007-7-12 21:31:40 > top of Java-index,Java Essentials,Java Programming...