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. Thanks, Paul LaBrier
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) System.exit(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, Manfred
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