Results 1 to 5 of 5
Thread: help
- 02-09-2010, 10:40 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 9
- Rep Power
- 0
help
Hi. Currently im using JOptionPane to create the program. This is the codes.
import javax.swing.JOptionPane;
public class Homework3
{
public static void main(String args[]){
int number;
String message1 = "Please enter the positive number"+ "\n";
String message2 = "Please enter the positive number, not negative number"+ "\n";
String message3 = "Please enter in numbers, not letters"+ "\n";
String num, outputMessage ;
try{
num = JOptionPane.showInputDialog(message1);
number = Integer.parseInt(num);
if (number<0)
throw new IllegalArgumentException();
outputMessage = "The number was"+ number ;
JOptionPane.showMessageDialog(null,outputMessage);
}
catch( NumberFormatException e)
{
JOptionPane.showMessageDialog(null,message3);
}
catch ( IllegalArgumentException e)
{
JOptionPane.showMessageDialog(null,message2);
}
System.exit(0);
}
}
The problem is, when I click the cancel button, it shows the message "Please enter in numbers, not letters". I'm so confusing that why the program shows this message and not closing when I click the cancel button. Also, the out put will shows in this way.
Lets say the output is 1234.
The number was 1234
The digits are
1
2
3
4
How do I allign the message in this way??? please help me!!!
- 02-10-2010, 12:21 AM #2
it shows message2 in the exception
maybe change this toJava Code:catch ( IllegalArgumentException e) { JOptionPane.showMessageDialog(null,message2); }
or, if you want the dialog thing,Java Code:catch ( IllegalArgumentException e) { System.err.println(e.getMessage()); e.printStackTrace(); }
Java Code:catch ( IllegalArgumentException e) { JOptionPane.showMessageDialog(null,e.getMessage()); }
- 02-10-2010, 03:18 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 9
- Rep Power
- 0
assignment due by tmr! HELP!
Hi. I need help for this program.
import javax.swing.JOptionPane;
public class Homework3
{
public static void main(String args[]){
int number;
String message1 = "Please enter the positive number"+ "\n";
String message2 = "Please enter the positive number, not negative number"+ "\n";
String message3 = "Please enter in numbers, not letters"+ "\n";
String num, outputMessage ;
try{
num = JOptionPane.showInputDialog(message1);
number = Integer.parseInt(num);
if (number<0)
throw new IllegalArgumentException();
outputMessage = "The number was"+ number ;
JOptionPane.showMessageDialog(null,outputMessage);
}
catch( NumberFormatException e)
{
JOptionPane.showMessageDialog(null,message3);
}
catch ( IllegalArgumentException e)
{
JOptionPane.showMessageDialog(null,message2);
}
System.exit(0);
}
}
So far its working but I want the Input to arrange in positive integer in range of 0 to 9999. It accepts the 4 digits of positive integer. and also once I put the positive integer in Input, the output has to be showing in this way.
The number was 1234
The digits are
1
2
3
4
How to write this program? please help me!!!!!!!
- 02-10-2010, 04:52 PM #4
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
Dnt repost the same question. It only makes you desperate for the answer, which is definetly a turn off for ppl to answer your question.
Last edited by [RaIdEn]; 02-10-2010 at 05:04 PM.
-


LinkBack URL
About LinkBacks

Bookmarks