473,543 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Objec t
{
public sum2() { // Creates new main
}
public static void main (String args[])
{
int intSum; // Holds the sum
intSum = 14 + 35; // Compute sum
JOptionPane.sho wMessageDialog( null, "The total is " + intSum); // Show
user the sum in a message window
System.exit(0); // Stops the execution
}
}

Jul 17 '05 #1
7 21603
try this:
import javax.swing.*; // Make Swing available
public class sum2 extends java.lang.Objec t
{
public sum2() { // Creates new main
}
public static void main (String args[])
{
int num1 = JOptionPane.sho wInputDialog("I nput the first number:");
int num2 = JOptionPane.sho wInputDialog("I nput the second number:");
int intSum; // Holds the sum
intSum = num1 + num2; // Compute sum
JOptionPane.sho wMessageDialog( *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.sho wInputDialog("E nter a number");
number1 = Double.parseDou ble(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.Objec t
{
public sum2() { // Creates new main
}
public static void main (String args[])
{
int intSum; // Holds the sum
intSum = 14 + 35; // Compute sum
JOptionPane.sho wMessageDialog( 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.goo glegroups.com.. .
try this:
import javax.swing.*; // Make Swing available
public class sum2 extends java.lang.Objec t
{
public sum2() { // Creates new main
}
public static void main (String args[])
{
int num1 = JOptionPane.sho wInputDialog("I nput the first number:");
int num2 = JOptionPane.sho wInputDialog("I nput the second number:");
int intSum; // Holds the sum
intSum = num1 + num2; // Compute sum
JOptionPane.sho wMessageDialog( *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.Objec t
{
public sum2() { // Creates new main
}
public static void main (String args[])
{
double num1 = JOptionPane.sho wInputDialog("I nput the first number:");
num1 = double.parseDou ble(num1)
double num2 = JOptionPane.sho wInputDialog("I nput the second number:");
num2 = double.parseDou ble(num2)
double numSum; // Holds the sum
numSum = num1 + num2; // Compute sum
JOptionPane.sho wMessageDialog( 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******** ************@co mcast.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.sho wInputDialog("E nter a number");
number1 = Double.parseDou ble(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.Objec t
{
public sum2() { // Creates new main
}
public static void main (String args[])
{
int intSum; // Holds the sum
intSum = 14 + 35; // Compute sum
JOptionPane.sho wMessageDialog( 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.goo glegroups.com.. .
try this:
import javax.swing.*; // Make Swing available
public class sum2 extends java.lang.Objec t
{
public sum2() { // Creates new main
}
public static void main (String args[])
{
int num1 = JOptionPane.sho wInputDialog("I nput the first number:");
int num2 = JOptionPane.sho wInputDialog("I nput the second number:");
int intSum; // Holds the sum
intSum = num1 + num2; // Compute sum
JOptionPane.sho wMessageDialog( *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.sho wInputDialog(St ring s) returns an Object of type
java.lang.Strin g so....
double num1 = JOptionPane.sho wInputDialog("I *nput the first number:");
must be changed to
String numstr1 = JOptionPane.sho wInputDialog("I *nput the first
number:"); andnum1 = double.parseDou ble(num1)

to
double num1=Double.par seDouble(numStr 1);
_______________ _______________ _______________ _______________ _______________ ______
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.sho wInputDialog("I *nput the first number:"); when I should
have posted Integer.parseIn t(JOptionPane.s howInputDialog( "I*nput 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
1410
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 the dialog's class from another class, I don't know how to retrieve the dialog input. I have a few text fields and can't reach them. I don't mind...
6
2604
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 enter a date. Becaue of this, the same information has to be entered twice which I would like to avoid. Is there a way using code, that I can prompt...
0
1141
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 but am not having any luck. Any ideas? ================================================================= The following example shows how to...
3
31799
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() for the input control. Do you know how to make this work in firefox? Thanks
5
1819
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 MessageBox. Instead, I would like to use a Textbox. Is there a way for me to accomplish this? Thanks in advance! Claude
7
5323
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 date System.out.println(string_dob); s.Info_DOB=Date.valueOf(string_dob); runs perfectly fine in the method insert() and throws up an...
3
5046
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 sentence, using JOptionPane.showInputDialog(). 2. The program will scan each letter in the string and count how many times the upper case letter 'E'...
1
3100
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 tabs at the top content area should be flush left and the light blue frame should line up under the logo graphic. Looks great in Safari and Firefox...
5
5586
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 tried doing If Payment = "" Then Exit sub, but it exits the sub if I press ok or cancel since the credit card value is not changed and remains to be...
0
7349
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7688
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5885
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5271
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
968
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
636
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.