Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Closed Thread
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2010, 10:40 AM
Member
 
Join Date: Jan 2010
Posts: 9
Rep Power: 0
keycoffee is on a distinguished road
Default 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!!!
Bookmark Post in Technorati
  #2 (permalink)  
Old 02-10-2010, 12:21 AM
travishein's Avatar
Senior Member
 
Join Date: Sep 2009
Location: Canada
Posts: 456
Rep Power: 1
travishein is on a distinguished road
Default
it shows message2 in the exception
Code:
catch ( IllegalArgumentException e)
{
JOptionPane.showMessageDialog(null,message2);
}
maybe change this to
Code:
catch ( IllegalArgumentException e)
{
System.err.println(e.getMessage());
e.printStackTrace();
}
or, if you want the dialog thing,

Code:
catch ( IllegalArgumentException e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
}
Bookmark Post in Technorati
  #3 (permalink)  
Old 02-10-2010, 03:18 PM
Member
 
Join Date: Jan 2010
Posts: 9
Rep Power: 0
keycoffee is on a distinguished road
Default 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!!!!!!!
Bookmark Post in Technorati
  #4 (permalink)  
Old 02-10-2010, 04:52 PM
Senior Member
 
Join Date: Oct 2009
Location: California,US
Posts: 195
Rep Power: 1
[RaIdEn] is on a distinguished road
Default
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.
Bookmark Post in Technorati
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 09:10 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org