Results 1 to 5 of 5

Thread: help

  1. #1
    keycoffee is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    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!!!

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    it shows message2 in the exception
    Java Code:
    catch ( IllegalArgumentException e)
    {
    JOptionPane.showMessageDialog(null,message2);
    }
    maybe change this to
    Java Code:
    catch ( IllegalArgumentException e)
    {
    System.err.println(e.getMessage());
    e.printStackTrace();
    }
    or, if you want the dialog thing,

    Java Code:
    catch ( IllegalArgumentException e)
    {
    JOptionPane.showMessageDialog(null,e.getMessage());
    }

  3. #3
    keycoffee is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    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!!!!!!!

  4. #4
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    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 06:04 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •