472,344 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,344 software developers and data experts.

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 21487
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: tkailler | last post by:
Hello, I've created a dialog by creating a class in which Instead of calling wx.Dialog.__init__ I precreate the dialog and then I create the GUI...
6
by: Mark | last post by:
Hi All, Access 2002 using Windows XP I am pretty new to writing code so please bear with me. I have some code which exectues an append and a...
0
by: DM | last post by:
This example copied from MSDN help does not work on any of my PC's. When the dialog opens, itdisplays a blank page. I'm trying to create a dialog...
3
by: oopaevah | last post by:
I want to have a separate button which invokes the "browse" button on an input type=file. In internet explorer the following code works ok, in...
5
by: Claude Grecea | last post by:
If anyone could help, I would greatly appreciated. I am using a "For Loop" and thru each complete iteration I would like it to stop and ask for...
7
helpwithcode
by: helpwithcode | last post by:
Hi people, I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements, String...
3
by: majorecono | last post by:
What I'm bad at is the dialog box part. This is the code Ive come up with... if you see a way to improve what im thinkng of.... post it please. ...
1
by: innivive | last post by:
I am having a problem with having margins display correctly in IE7, Firefox and Safari. I am not sure if it is the "double margin error" or...
5
by: snehadulur | last post by:
In the input box if we click ok or cancel the invoice prints. By default the credit card info is 0, and most of the cases its 0 even when we want to...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.