469,282 Members | 2,000 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Help: Input Dialog Box

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
}
}

Jul 17 '05 #1
7 21317
try this:
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 num1 = JOptionPane.showInputDialog("Input the first number:");
int num2 = JOptionPane.showInputDialog("Input the second number:");
int intSum; // Holds the sum
intSum = num1 + num2; // Compute sum
JOptionPane.showMessageDialog(*null, "The total is " + intSum);
// Show
user the sum in a message window
System.exit(0); // Stops the execution
}

Jul 17 '05 #2
Hi, I hacked on this one a bit and noticed it has some errors, the compiler
doesnt like the datatypes. Then I noticed I should have mentioned the
recommeded approach that is suggested to use I should have made mention of
earlier, sorry about that. So I tried to incorporatte what you suggested
below with the recommendation, and made a mess out of it. Here is the
recomended approach to apply to the good code to modify:

(BTW, int seemed like a good idea, why double over int?)

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

I'm to weld into this existig program that does compile fine:

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
}
}

"sgt_sock" <sg*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
try this:
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 num1 = JOptionPane.showInputDialog("Input the first number:");
int num2 = JOptionPane.showInputDialog("Input the second number:");
int intSum; // Holds the sum
intSum = num1 + num2; // Compute sum
JOptionPane.showMessageDialog(*null, "The total is " + intSum);
// Show
user the sum in a message window
System.exit(0); // Stops the execution
}
Jul 17 '05 #3
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.

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" <Ba***************@NOSPAMyahoo.com> wrote in message
news:VI********************@comcast.com...
Hi, I hacked on this one a bit and noticed it has some errors, the
compiler doesnt like the datatypes. Then I noticed I should have
mentioned the recommeded approach that is suggested to use I should have
made mention of earlier, sorry about that. So I tried to incorporatte
what you suggested below with the recommendation, and made a mess out of
it. Here is the recomended approach to apply to the good code to modify:

(BTW, int seemed like a good idea, why double over int?)

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

I'm to weld into this existig program that does compile fine:

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
}
}

"sgt_sock" <sg*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
try this:
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 num1 = JOptionPane.showInputDialog("Input the first number:");
int num2 = JOptionPane.showInputDialog("Input the second number:");
int intSum; // Holds the sum
intSum = num1 + num2; // Compute sum
JOptionPane.showMessageDialog(*null, "The total is " + intSum);
// Show
user the sum in a message window
System.exit(0); // Stops the execution
}

Jul 17 '05 #4
Basil Fawlty wrote:
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.


Sorry to be so harsh, but no it is not close. Speak to your instructor,
read the textbook, read the javadoc.

Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #5
I think you got some problem understanding the "java" language. Sorry
but you must be knowing that
JOptionPane.showInputDialog(String s) returns an Object of type
java.lang.String so....
double num1 = JOptionPane.showInputDialog("I*nput the first number:");
must be changed to
String numstr1 = JOptionPane.showInputDialog("I*nput the first
number:"); andnum1 = double.parseDouble(num1)

to
double num1=Double.parseDouble(numStr1);
__________________________________________________ _______________________________
Any ways this may not solve all your problems as far as you keep typing
Double as double and many other things are there to mention. So please
do what Ray is saying.

Viator :-)

Jul 17 '05 #6
If you just want to make it work, there is a way to make JOptionPane
show a input dialog, I read it here
http://java.sun.com/docs/books/tutor....html#features
look for the header "Getting the User's Input from a Dialog".

On the other hand if you really want to make a program. You should
consider reading a little. i think there is a program like this on the
swing tutorial, you will need a frame and a place to put the buttons,
etc. Because right now you are just poping up windows. For a swing
tutorial i recomend the one at sun's website
http://java.sun.com/docs/books/tutor...ng/components/

Jul 17 '05 #7
That was somewhat of a mistake on my part. I posted
JOptionPane.showInputDialog("I*nput the first number:"); when I should
have posted Integer.parseInt(JOptionPane.showInputDialog("I*np ut the
first number:")); This will convert the String to an integer, so the
data types will match. sorry about that.

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Mark | last post: by
5 posts views Thread by Claude Grecea | last post: by
1 post views Thread by innivive | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.