Results 1 to 16 of 16
- 09-27-2008, 02:11 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
[SOLVED] a method for checking string or char , try catch
i really appreciate if someone can help me ,below is my source code
i need modify my source code with this, try catch , to check if user input letter then error message pop outint 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);
}
}
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 )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");
}
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);
}}
-
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"); }
- 09-27-2008, 02:25 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
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 ?
-
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.
- 09-27-2008, 02:33 AM #5
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
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?
-
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.
- 09-27-2008, 02:49 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
oh ! , ok ...thank for your help... erm , how do i format my post code ^^" ?
-
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.
- 09-27-2008, 03:13 AM #9
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
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"); } }
-
Well done! You appear to have a fine future ahead of you in programming! :)
- 09-27-2008, 04:31 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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"); } }
- 09-27-2008, 05:16 AM #12
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
- 09-27-2008, 12:03 PM #13
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
ono ! i need help again =(...
because i using these source code
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 ?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"); } }
- 09-27-2008, 02:40 PM #14
You need to handle the WindowEvent created when you close the window and execute a System.exit() call.clicked on "cancel " or " close button " , but it did not work
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.
- 09-28-2008, 07:29 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
On which buttons are you talking about, on the message box?
- 09-29-2008, 07:18 AM #16
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
How can i insert a char into a string
By Jamie in forum New To JavaReplies: 8Last Post: 02-17-2011, 08:59 PM -
char to string
By kian_hong2000 in forum New To JavaReplies: 2Last Post: 08-25-2008, 01:51 PM -
Assigning a string value to a char
By coffeebean in forum New To JavaReplies: 4Last Post: 06-15-2008, 06:30 AM -
Char to String in java
By trill in forum New To JavaReplies: 1Last Post: 08-01-2007, 01:42 PM -
Help with, String, Char
By lenny in forum New To JavaReplies: 1Last Post: 07-25-2007, 02:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks