Learning some swing operations

I'm working on a program that is using swing. In the getDouble method, I believe what I want to do is:

1. allow for user input

2. capture the user input

I'm not sure how to do this in this program:

import javax.swing.*; // Package containing JOptionPane

public class KmToMilesMethods {

//========================================================= constants

private static final double MILES_PER_KILOMETER = 0.621;

//============================================================== main

public static void main(String[] args) {

double kms = getDouble("Enter number of kilometers.");

double miles = convertKmToMi(kms);

displayString(kms + " kilometers is " + miles + " miles.");

}

//===================================================== convertKmToMi

// Conversion method - kilometers to miles.

private static double convertKmToMi(double kilometers) {

double miles = kilometers * MILES_PER_KILOMETER;

return miles;

// added two Java lines above

}

//========================================================= getDouble

// I/O convenience method to read a double value.

private static double getDouble(String prompt) {

//three Java lines go here

}

//===================================================== displayString

// I/O convenience method to display a string in dialog box.

private static void displayString(String output) {

JOptionPane.showMessageDialog(null, output);

**************

So far I've tried to enter a showInputDialog using a string, double, kms or number and I'm getting errors that I can't convert string to double or that I can't resolve one of the variables "kms"

Any help in steering my brain down the right thought path is appreciated.

Dansing

[1871 byte] By [dansinga] at [2007-11-27 0:20:57]
# 1

Try getting the input from a JOptionPane.showInputDialog() as a string, then use Float.parseFloat() while catching a number format exception.

example:

float input, result;

String number = JOptionPane.showInputDialog(null, "Enter a number");

try

{

input = Float.parseFloat(number);

JOptionPane.showMessageDialog(null, "number is: " + input);

}

catch (NumberFormatException ex)

{

JOptionPane.showMessageDialog(null, "Must enter a number");

System.exit(0);

}

Joe

Joe_ha at 2007-7-11 22:14:07 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks for your response...but I don't think that will do it. You see the main method has the parameter to "enter number of kilometers"...and it's calling the getDouble method. In the getDouble method, I want to allow the user to enter the input and then I want to capture it, parse it to a double (because the return for the getDouble method must be a double. Maybe I'm looking at this wrong. I'm supposed to add 3 lines of code that will make the getDouble method work with the program.

Thanks, Dansing

dansinga at 2007-7-11 22:14:07 > top of Java-index,Java Essentials,New To Java...
# 3

> Thanks for your response...but I don't think that

> will do it.

Why not? I think that's exactly what you're supposed to do. Here's the three lines of code you need (in pseudocode, of course)

1) Collect input from JOptionPane into a String

2) Parse String into a double

3) Return the double

That's all you need to do.

CaptainMorgan08a at 2007-7-11 22:14:07 > top of Java-index,Java Essentials,New To Java...
# 4

>That's all you need to do.

Thanks folks! You're right...I couldn't see the trees thru the forest. I'm just learning this stuff. I added these 3 lines of code and it now works...

>1) Collect input from JOptionPane into a String

>2) Parse String into a double

>3) Return the double

String kmStr = JOptionPane.showInputDialog(prompt);

double kms = Double.parseDouble(kmStr);

return kms;

After I added this code, I couldn't figure out how to get the words "enter number of kilometers" to be in the dialog box. It was set up in the parameters in the call to the method from main, but it wouldn't call it until I put the word "prompt" in the parameter for the String assignment (JOptionPane.showInputDialog(prompt). I was getting errors in the main about the String whenever I fooled with the method!

Anyways...thanks alot...you've both helped me think thru this! Happy Easter!

Dansing

dansinga at 2007-7-11 22:14:07 > top of Java-index,Java Essentials,New To Java...
# 5

> After I added this code, I couldn't figure out how to

> get the words "enter number of kilometers" to be in

> the dialog box. It was set up in the parameters in

> the call to the method from main, but it wouldn't

> call it until I put the word "prompt" in the

> parameter for the String assignment

> (JOptionPane.showInputDialog(prompt). I was getting

> errors in the main about the String whenever I fooled

> with the method!

try using this method of JOptionPane

showInputDialog(Component parentComponent, Object message, String title, int messageType)

Where Object Message is a string with the message u want, String Title is the title of the JOptionPane, and int messageType is the Icon to display, something like this

showInputDialog(this, "Enter Kilometers", "Kilometers Entry", JOptionPane.QUESTION_MESSAGE);

cheers, Happy Easter

Octaclota at 2007-7-11 22:14:07 > top of Java-index,Java Essentials,New To Java...