Results 1 to 16 of 16
  1. #1
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Default [SOLVED] a method for checking string or char , try catch

    i really appreciate if someone can help me ,below is my source code
    int goal,again , ask;
    final double INITIAL_SALES = 0.00;


    double monthly_sales =0;
    ask = JOptionPane.showConfirmDialog (null, "Do you wish to start Sales amount program ? ");
    do
    {



    if(ask == JOptionPane.YES_OPTION){


    String base_salary_string = JOptionPane.showInputDialog("please enter your base salary (RM): ");
    int base_salary = Integer.parseInt(base_salary_string);



    String initial_goalString = JOptionPane.showInputDialog("please enter your goal (RM) (in one year, 12 months): ");
    int initial_goal = Integer.parseInt(initial_goalString);

    String min_sales2String = JOptionPane.showInputDialog("please enter minumum of sales for 10 % commission (RM): ");
    double min_sales2 = Double.parseDouble(min_sales2String);

    String min_sales3String = JOptionPane.showInputDialog("please enter minimum of sales for 12 % commission (RM): ");
    double min_sales3 = Double.parseDouble(min_sales3String);



    goal= initial_goal - base_salary; //actual goal is initial goal - salary


    double Commission = 0; //initilize commission to 0
    double salesAmount = INITIAL_SALES;

    do {
    // Increase salesAmount by 1 cent
    salesAmount += 0.01;

    if (salesAmount >= min_sales3) //minimum sales which will get 12% commision rate
    Commission = (base_salary * 0.08) + (base_salary * 0.1) + ((salesAmount - 10000) * 0.12);
    else if
    (salesAmount >= min_sales2) //minimum sales which will get 10% commision rate
    Commission = (base_salary * 0.08) + ((salesAmount - 5000) * 0.10);
    else
    Commission = salesAmount * 0.08; // 8% commision rate
    } while (Commission <goal);

    monthly_sales = salesAmount /12; // sales amount are divide into 12 months


    String output=String.format("\nThe sales amount RM %.2f is required to make a commision of RM %d\nmonthly sales amount is needed to make is RM %.2f ", salesAmount, goal, monthly_sales);
    JOptionPane.showMessageDialog(null, output);
    }
    ask = JOptionPane.showConfirmDialog (null, "Wish to continue ?");
    }
    while (ask == JOptionPane.YES_OPTION);
    }

    }
    i need modify my source code with this, try catch , to check if user input letter then error message pop out

    String base_salary_string = JOptionPane.showInputDialog("please enter your base salary (RM): ");
    try
    {
    int base_salary = Integer.parseInt(base_salary_string);
    }
    // If base_salary isn't an int, it calls a NumberFormatException
    catch (NumberFormatException e)
    {
    JOptionPane.showMessageDialog(null, "you have entered invalid data");
    }
    but after i modified it , because it seem error occur that here , the symbol of base salary cant be found , please tell me how do i correct this thing , i am beginner (Ps: the highlighted is modify code )
    int goal,again , ask;
    final double INITIAL_SALES = 0.00;


    double monthly_sales =0;
    ask = JOptionPane.showConfirmDialog (null, "Do you wish to start Sales amount program ? ");
    do
    {



    if(ask == JOptionPane.YES_OPTION){


    String base_salary_string = JOptionPane.showInputDialog("please enter your base salary (RM): ");
    try
    {
    int base_salary = Integer.parseInt(base_salary_string);
    }
    // If base_salary isn't an int, it calls a NumberFormatException
    catch (NumberFormatException e)
    {
    JOptionPane.showMessageDialog(null, "you have entered invalid data");
    }



    String initial_goalString = JOptionPane.showInputDialog("please enter your goal (RM) (in one year, 12 months): ");
    int initial_goal = Integer.parseInt(initial_goalString);

    String min_sales2String = JOptionPane.showInputDialog("please enter minumum of sales for 10 % commission (RM): ");
    double min_sales2 = Double.parseDouble(min_sales2String);

    String min_sales3String = JOptionPane.showInputDialog("please enter minimum of sales for 12 % commission (RM): ");
    double min_sales3 = Double.parseDouble(min_sales3String);



    goal= initial_goal - base_salary; //actual goal is initial goal - salary


    double Commission = 0; //initilize commission to 0
    double salesAmount = INITIAL_SALES;

    do {
    // Increase salesAmount by 1 cent
    salesAmount += 0.01;

    if (salesAmount >= min_sales3) //minimum sales which will get 12% commision rate
    Commission = (base_salary * 0.08) + (base_salary * 0.1) + ((salesAmount - 10000) * 0.12);
    else if
    (salesAmount >= min_sales2) //minimum sales which will get 10% commision rate
    Commission = (base_salary * 0.08) + ((salesAmount - 5000) * 0.10);
    else
    Commission = salesAmount * 0.08; // 8% commision rate
    } while (Commission <goal);

    monthly_sales = salesAmount /12; // sales amount are divide into 12 months


    String output=String.format("\nThe sales amount RM %.2f is required to make a commision of RM %d\nmonthly sales amount is needed to make is RM %.2f ", salesAmount, goal, monthly_sales);
    JOptionPane.showMessageDialog(null, output);
    }
    ask = JOptionPane.showConfirmDialog (null, "Wish to continue ?");
    }
    while (ask == JOptionPane.YES_OPTION);
    }}

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

    Default

    Declare the int variable before the try/catch block. That's the key:
    Java Code:
    String base_salary_string = JOptionPane.showInputDialog("please enter your base salary (RM): ");
    int base_salary = 0;
    try
    {
       base_salary = Integer.parseInt(base_salary_string);
       // do stuff here that you'll only do if the above was successful
    }
    catch (NumberFormatException e)
    {
      JOptionPane.showMessageDialog(null, "you have entered invalid data");
    }

  3. #3
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Default

    wow , i have just posted it a couple of minutes ago... i didnt expect i get the answer so soon... yeah , thank !! i tested it out and it works !! .... but if error message pop out ( mean user input invalid data ) , is there a way it can jump out the statement and terminate ?

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

    Default

    Are you sure you wish to do this? Most apps of this nature will continue to re-request the information until the user either enters the correct data or indicates that he wishes to terminate.

  5. #5
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Default

    o ! yeah ... now you remind me ! ... then how do i modify that it will continue to re-request the information until the user either enters the correct data or indicates that he wishes to terminate?

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

    Default

    I have to warn you that you will never get anywhere asking this without trying to do it yourself first. So please give it a try, if it fails, try to fix it, and again if it fails, find the errors and try to fix them. Only THEN if you are still having a problem, come back with your code and question.

    Also, please when posting code, please see that it is formatted correctly first before pasting it. Your current code is hard to read.

  7. #7
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Default

    oh ! , ok ...thank for your help... erm , how do i format my post code ^^" ?

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

    Default

    You've got the code tags on, so thanks for doing that, but you need to post code that is properly indented. Yours is not.

  9. #9
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Default

    sorry mate , i quoted them because i need to highlighted those which need to be modified.... oh yeah i have come up with a way make it will continue to re-request the information until the user either enters the correct data after user input invalid data ;)

    Java Code:
      boolean loop = true;
    double min_sales2 = 0;
    while (loop) {
       String min_sales2String = JOptionPane.showInputDialog("please enter minumum of sales for 10 % commission (RM): ");
       try {
           min_sales2 = Double.parseDouble(min_sales2String);
           loop = false;
       }
       catch (NumberFormatException e) {
          JOptionPane.showMessageDialog(null, "you have entered invalid data");
       }
    
    }

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

    Default

    Well done! You appear to have a fine future ahead of you in programming! :)

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    In much better way, try to declaring variables only once. It's a good practice in future. See your code with a simple modification below.

    Java Code:
            boolean loop = true;
            double min_sales2 = 0;
            String min_sales2String;
    
            while (loop) {
                min_sales2String = JOptionPane.showInputDialog("please enter minumum of sales for 10 % commission (RM): ");
                try {
                    min_sales2 = Double.parseDouble(min_sales2String);
                    loop = false;
                }
                catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "you have entered invalid data");
                }
            }

  12. #12
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Well done! You appear to have a fine future ahead of you in programming! :)
    thank for your compliment , i would not have done it without you help ! thank again !

    Quote Originally Posted by Eranga View Post
    In much better way, try to declaring variables only once. It's a good practice in future. See your code with a simple modification below.

    Java Code:
            boolean loop = true;
            double min_sales2 = 0;
            String min_sales2String;
    
            while (loop) {
                min_sales2String = JOptionPane.showInputDialog("please enter minumum of sales for 10 % commission (RM): ");
                try {
                    min_sales2 = Double.parseDouble(min_sales2String);
                    loop = false;
                }
                catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "you have entered invalid data");
                }
            }
    thank for you advice ! i will try to do that..:D

    problem solved , post closed :)

  13. #13
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Unhappy

    ono ! i need help again =(...

    because i using these source code

    Java Code:
        double min_sales2 = 0;
            String min_sales2String;
    
            while (loop) {
                min_sales2String = JOptionPane.showInputDialog("please enter minumum of sales for 10 % commission (RM): ");
                try {
                    min_sales2 = Double.parseDouble(min_sales2String);
                    loop = false;
                }
                catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "you have entered invalid data");
                }
            }
    it will continue to re-request the information until the user enters the correct data after user input invalid data.. because user cannot terminate the program unless he enter all the correct data! i had clicked on "cancel " or " close button " , but it did not work ... i really don't have an idea about this , just started to learn java 3 weeks ago... can someone teach me how to terminate this ?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    clicked on "cancel " or " close button " , but it did not work
    You need to handle the WindowEvent created when you close the window and execute a System.exit() call.
    If you are using JFrame, it has a method: setDefaultCloseAction(??) that you can use to exit the app. Read the API doc for correct spelling and what args to use.
    If you are NOT using JFrame, then you will need to add a WindowListener and use the windowClosing method to trap and handle the closing.
    Use search to find sample codes for these.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  16. #16
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

Similar Threads

  1. How can i insert a char into a string
    By Jamie in forum New To Java
    Replies: 8
    Last Post: 02-17-2011, 09:59 PM
  2. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 02:51 PM
  3. Assigning a string value to a char
    By coffeebean in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 07:30 AM
  4. Char to String in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 02:42 PM
  5. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 03:58 PM

Posting Permissions

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