my dog learned polymorphism
The moose likes Beginning Java and the fly likes input multiple strings using JOptionPane.showInputDialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "input multiple strings using JOptionPane.showInputDialog" Watch "input multiple strings using JOptionPane.showInputDialog" New topic

input multiple strings using JOptionPane.showInputDialog

Paul LaBrier

Joined: Apr 08, 2002
Posts: 21
I am pretty sure the answer to my question will be NO, but I will ask anyway.
For a homework assignment in my Into to Java Class I have to write a simple program that will take user input in the for of a string for gallons and miles driven and compute the MPG. I have the program doing what I need it to for the assignment, but I would like to go a small step further. I do not like the fact that it is using two dialog boxes to input the two numbers. Is there any way, still using "JOptionPane.showInputDialog" ( or something close to it) to request both strings within the same dialog box? I know that I could make a custom dialog box, but that is well beyond the scope of the assignment.
Below is a snippet of code showing what I am currently doing.
// input new value for miles and
// convert from String to int
inputMiles = JOptionPane.showInputDialog(
"Enter miles (-1 to quit):" );
miles = Integer.parseInt( inputMiles );
// read second number from user as String
inputGallons = JOptionPane.showInputDialog(
"Enter gallons:" );
Again, I have the assignment done, just looking to improve it.
Paul LaBrier
Manfred Mueller

Joined: Jul 18, 2002
Posts: 10
Hi Paul,
if you do not insist on two input fields, you might want to use this solution: prompt the user to enter both the gallons and the miles into the single input field with a separator between the both values (a blank character will do). Then let a java.util.StringTokenizer object parse the input string appropriately. This will do (although the custom dialog you mentionend would be a more elegant solution ).
And here's the code snippet:
String input = JOptionPane.showInputDialog("Enter <gallons>/<miles> (leave field empty to quit):" );
// check for quitting condition
if (input.length() == 0)
StringTokenizer st = new StringTokenizer(input, " /,|"); // allow for several separators
if (st.hasMoreElements())
inputGallons = (String)st.nextElement();
if (st.hasMoreElements())
inputMiles = (String)st.nextElement();
// handle possible errors (miles missing, wrong separator...)
Just don't forget to
import java.util.StringTokenizer;
Regards and have fun with Java,
sharad sinhaabc

Joined: Jul 30, 2009
Posts: 1
hi friend
taking multiple fields in dilog box is a feature

JTextField jf = new JTextField();
String user1 =javax.swing.JOptionPane.showInputDialog(null,jf ,"PLEASE GIVE YOUR INPUT", JOptionPane.OK_CANCEL_OPTION);
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10768

Welcome to the Ranch

Please don't wake the zombies.
Did you notice, the original thread is from 2002? I doubt the original poster would wait 7 years for an answer
To get an idea of how to get the most out of the Ranch, please do take out time to read these guidelines

Oh yes. Also please check your private messages for an important administrative matter

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
subject: input multiple strings using JOptionPane.showInputDialog
jQuery in Action, 3rd edition