wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Help with JOptionPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with JOptionPane" Watch "Help with JOptionPane" New topic
Author

Help with JOptionPane

c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
I am having problems with displaying the second input dialog. Can someone help me!

import javax.swing.JOptionPane;

public class GetInputFromKeyboardJOptionPane{

public static void main(String[] args) {
String name = "";

name = JOptionPane.showInputDialog("Please enter your name");
String msg = "Hello " + name + "!";
JOptionPane.showMessageDialog(null, msg);

String age = "";
int ageint = Integer.parseInt(age);

age = JOptionPane.showInputDialog("Please enter your age");
if (ageint > 100){
String msg2 = "Hello " + name + "You are old!";
JOptionPane.showMessageDialog(null, msg2);
}else{
String msg3 = "Hello " + name + "You are young!";
JOptionPane.showMessageDialog(null, msg3);
}

}
}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
One problem I see is this.



Did you get a NumberFormatException?
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
No. No error. The first part (Please enter your name) works.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
After you click OK in the message dialog, what happens?
Bhabani Sankar Mishra
Greenhorn

Joined: Mar 28, 2006
Posts: 3
You are using:
String age = "";
int ageint = Integer.parseInt(age);
age = JOptionPane.showInputDialog("Please enter your age");
Instead use :
String age = "";
age = JOptionPane.showInputDialog("Please enter your age");
int ageint = Integer.parseInt(age);
See is the problem is solved


IIT rox
c york
Greenhorn

Joined: Mar 16, 2006
Posts: 28
Yes, it worked!

Thank You so much. It does make sense to get the age then convert from a string to an int. Sometimes 4 eyes are better than 2.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with JOptionPane