Help: Input Dialog Box

B

Basil Fawlty

I need help and suggestions on how to modify the below program to get two
numbers from user input using a input dialog box. B/c I'm a total newbie
with next to no programming experience, I'm not sure where to insert the
additional code. I'm using Forte for Java 3.0 CE. The below code does
compile and run. Please advise.

import javax.swing.*; // Make Swing available
public class sum2 extends java.lang.Object
{
public sum2() { // Creates new main
}
public static void main (String args[])
{
int intSum; // Holds the sum
intSum = 14 + 35; // Compute sum
JOptionPane.showMessageDialog(null, "The total is " + intSum); // Show
user the sum in a message window
System.exit(0); // Stops the execution
}
}
 
B

Basil Fawlty

More detail, I should have posted this info too, sorry, I'm to use a
recommeded approach to modify the previous code:

String input;
double number1;
input = JoptionPane.showInputDialog("Enter a number");
number1 = Double.parseDouble(input);

But when I do it seems to give me a data type mistach, or it may be don to
me making a mess out of the good code for not know the correct syntactical
aproach. Can someone please help me?
 
B

Basil Fawlty

This is as far as I can get it now, below, I have compile errors, datatype
mismatch (maybe b/v of string args) and a problem with the double.parse, I
dont know enough to mod the code further w/o making a mess, I know its
close. HELP!

import javax.swing.*; // Make Swing available
public class sum2 extends java.lang.Object
{
public sum2() { // Creates new main
}
public static void main (String args[])
{
double num1 = JOptionPane.showInputDialog("Input the first number:");
num1 = double.parseDouble(num1)
double num2 = JOptionPane.showInputDialog("Input the second number:");
num2 = double.parseDouble(num2)
double numSum; // Holds the sum
numSum = num1 + num2; // Compute sum
JOptionPane.showMessageDialog(null, "The total is " + numSum);
// Show user the sum in a message window
System.exit(0); // Stops the execution
}
}


Basil Fawlty said:
More detail, I should have posted this info too, sorry, I'm to use a
recommeded approach to modify the previous code:

String input;
double number1;
input = JoptionPane.showInputDialog("Enter a number");
number1 = Double.parseDouble(input);

But when I do it seems to give me a data type mistach, or it may be don to
me making a mess out of the good code for not know the correct syntactical
aproach. Can someone please help me?

Basil Fawlty said:
I need help and suggestions on how to modify the below program to get two
numbers from user input using a input dialog box. B/c I'm a total newbie
with next to no programming experience, I'm not sure where to insert the
additional code. I'm using Forte for Java 3.0 CE. The below code does
compile and run. Please advise.

import javax.swing.*; // Make Swing available
public class sum2 extends java.lang.Object
{
public sum2() { // Creates new main
}
public static void main (String args[])
{
int intSum; // Holds the sum
intSum = 14 + 35; // Compute sum
JOptionPane.showMessageDialog(null, "The total is " + intSum); //
Show
user the sum in a message window
System.exit(0); // Stops the execution
}
}
 
J

jfalt

Looks like you were closer before. In your latest iteration, you have
:
double num1 = JOptionPane.showInputDialog("I­nput the first number:");
You are attempting to assign a String Object into a double. Try
combining your lines such as double num1 =
Double.parseDouble(JOptionPane.showInputDialog(" ..."));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top