473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 21589
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 dialog using the Create method. When I create...
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 select query. Both queries require the user to...
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 that calls a function in the page that opened it...
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 firefox nothing happens. All I do is call click()...
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 user input, but I would not like to use a...
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 string_dob=text_dob.getText(); //Converting string to...
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. Program should.... 1. Ask user to type in a...
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 something else. Any help would be appreciated. The file...
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 take a print out i.e., the value is changed. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.