|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-02-2008, 06:41 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 34
|
|
|
Error with my Try Catch
Hello to all
I am not receiving any compiling errors with my code. But my code is not printing out the error message in the JOptionPane window and I can't figure out why. Here is my code. My objective is.
If anything other than a number is entered including no input at all, then the warning joption pane window should appear with the message I have below in my code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class StaffingProgram extends JFrame
{
private JLabel resultAL, resultBL, resultCL, outputAL, outputBL, outputCL;
private JTextField resultATF, resultBTF, resultCTF, outputATF, outputBTF, outputCTF;
private JButton calculateB, exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
private static final int WIDTH = 900;
private static final int HEIGHT = 600;
public StaffingProgram()
{
resultAL = new JLabel("Please enter the number of employees presently on duty at location A: ",
SwingConstants.RIGHT);
resultBL = new JLabel("Please enter the number of employees presently on duty at location B: ",
SwingConstants.RIGHT);
resultCL = new JLabel("Please enter the number of employees presently on duty at location C: ",
SwingConstants.RIGHT);
outputAL = new JLabel("Number of employees under or over for location A: ",
SwingConstants.RIGHT);
outputBL = new JLabel("Number of employees under or over for location B: ",
SwingConstants.RIGHT);
outputCL = new JLabel("Number of employees under or over for location C: ",
SwingConstants.RIGHT);
resultATF = new JTextField(10);
resultBTF = new JTextField(10);
resultCTF = new JTextField(10);
outputATF = new JTextField(10);
outputBTF = new JTextField(10);
outputCTF = new JTextField(10);
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
setTitle("Staffing Level Calculations");
Container pane = getContentPane();
pane.setLayout(new GridLayout(7, 2));
pane.add(resultAL);
pane.add(resultATF);
pane.add(resultBL);
pane.add(resultBTF);
pane.add(resultCL);
pane.add(resultCTF);
pane.add(outputAL);
pane.add(outputATF);
pane.add(outputBL);
pane.add(outputBTF);
pane.add(outputCL);
pane.add(outputCTF);
pane.add(calculateB);
pane.add(exitB);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double resulta, resultb, resultc;
resulta = Double.parseDouble(resultATF.getText());
resultb = Double.parseDouble(resultBTF.getText());
resultc = Double.parseDouble(resultCTF.getText());
try
{
if (resulta > 5){
resulta = resulta - 5;
outputATF.setText("You are overstaffed by " + resulta);
}
else if (resulta < 5){
resulta = 5 - resulta;
outputATF.setText("You are understaffed by " + resulta);
}
else if (resulta == 5){
outputATF.setText("Your staffing is sufficient");
}
if (resultb > 8){
resultb = resultb - 8;
outputBTF.setText("You are overstaffed by " + resultb);
}
else if (resultb < 8){
resultb = 8 - resultb;
outputBTF.setText("You are understaffed by " + resultb);
}
else if (resultb == 8){
outputBTF.setText("Your staffing is sufficient");
}
if (resultc > 10){
resultc = resultc - 10;
outputCTF.setText("You are overstaffed by " + resultc);
}
else if (resultc < 10){
resultc = 10 - resultc;
outputCTF.setText("You are understaffed by " + resultc);
}
else if (resultc == 10){
outputCTF.setText("Your staffing is sufficient");
}
else
{
throw new TheException(); //calls
}
}
catch (TheException ev)
{
JOptionPane.showMessageDialog(null, "" +ev + " You must enter a number", "Invalid Input",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
class TheException extends Exception
{
public String toString()
{
return "Illegal";
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args) throws IOException
{
StaffingProgram rectObject = new StaffingProgram();
}
}
|
|

05-02-2008, 07:17 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
Because catch the exception in wrong place.
resulta = Double.parseDouble(resultATF.getText());
What can be here, if your input is not a number you get NumberFormatException. Your try-catch block not caught such thing.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-02-2008, 07:18 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
ev in TheException is just a user-defined object....
It doesn't automatically convert to String....
Unless, you call the toString() method....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-02-2008, 07:44 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
But still, I'm not sure whether his try-catch can be caught such an exception sukatoa. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-02-2008, 08:01 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
He needs to be catched that NumberFormatException.....
That TheException may be catched if ( i don't know what he is trying to do with that ).... 
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-02-2008, 08:05 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
Yes, that's why I worried too sukato. See his try block. Just comparing integer value, and if he fails throw the exception. So, what he going to do.
Basically he's dealing with numbers, and the most perfect thing is to catch the number format exception. That mean he has to check that given data is valid or not.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-02-2008, 08:13 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
|
I think its funny that the part that needs to be in a try catch isn't. You should put the parsing into a number format Exception. Then in your else statement display the info the error info.
__________________
My IP address is 127.0.0.1
|
|

05-02-2008, 08:17 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
Basically he's dealing with numbers, and the most perfect thing is to catch the number format exception. That mean he has to check that given data is valid or not.
Yap.... absolutely... 
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-02-2008, 05:35 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 34
|
|
|
Error with my Try Catch
Eranga
Zosden
Sukatoa
Thank you for your input. I am not very familiar with what you are talking about so I did a little reading online about the number format exception. I changed my coding up a bit and put the parsing within the try statement and changed it.
Double ---> Integer and placed within try catch statement
Deleted ---> NumberFormatException extends Exception section
This worked out great. Thank you all for your support and help on this. All of us have our own way of learning and mine unfortunately requires a lot of help and pointing in the right direction. I am not that good at reading between the lines.  I needed to hear the number format exception and the statement about placing the parsing into the try statement in order to solve this.
Thanks again. here is what I did.
try
{
resulta = Integer.parseInt(resultATF.getText());
resultb = Integer.parseInt(resultBTF.getText());
resultc = Integer.parseInt(resultCTF.getText());
if (resulta > 5){
resulta = resulta - 5;
outputATF.setText("You are overstaffed by " + resulta);
}
else if (resulta < 5){
resulta = 5 - resulta;
outputATF.setText("You are understaffed by " + resulta);
}
else if (resulta == 5){
outputATF.setText("Your staffing is sufficient");
}
if (resultb > 8){
resultb = resultb - 8;
outputBTF.setText("You are overstaffed by " + resultb);
}
else if (resultb < 8){
resultb = 8 - resultb;
outputBTF.setText("You are understaffed by " + resultb);
}
else if (resultb == 8){
outputBTF.setText("Your staffing is sufficient");
}
if (resultc > 10){
resultc = resultc - 10;
outputCTF.setText("You are overstaffed by " + resultc);
}
else if (resultc < 10){
resultc = 10 - resultc;
outputCTF.setText("You are understaffed by " + resultc);
}
else if (resultc == 10){
outputCTF.setText("Your staffing is sufficient");
}
else
{
throw new NumberFormatException(); //calls
}
}
catch (NumberFormatException ev)
{
JOptionPane.showMessageDialog(null, "" +ev + " You must enter a number", "Invalid Input",
JOptionPane.INFORMATION_MESSAGE);
}
|
|

05-03-2008, 05:38 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
Again, your exception doesn't convert into string. Try something like this.
JOptionPane.showMessageDialog(null, "" + ev.getMessage() + " You must enter a number", "Invalid Input", JOptionPane.INFORMATION_MESSAGE);
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|