Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 30th, 2008, 3:33 PM   #1
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 53
Rep Power: 1 kewlgeye is on a distinguished road
Java Error Messages

I received an error. I have posted a pic of the errors that I am receiving. Can someone please have a look at this and show me what I am doing wrong? Here is the code and then the pic.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

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);}
			if (resulta < 5)
			{resulta = 5 - resulta;
			outputATF.setText("You are understaffed by " + resulta);}
			if (resulta == 5)
			{outputATF.setText("Your staffing is sufficient");}
	
			else
			{
			throw new TheException(); //calls
            		}
		
		

			if (resultb > 8)
			{resultb = resultb - 8;
			outputBTF.setText("You are overstaffed by " + resultb);
			if (resultb < 8)
			resultb = 8 - resultb;
			outputBTF.setText("You are understaffed by " + resultb);
			if (resultb == 8)
			outputBTF.setText("Your staffing is sufficient");
			
			else
			{
			throw new TheException(); //calls
            		}	
			

			if (resultc > 10)
			{resultc = resultc - 10;
			outputCTF.setText("You are overstaffed by " + resultc);}
			if (resultc < 10)
			{resultc = 10 - resultc;
			outputCTF.setText("You are understaffed by " + resultc);}
			if (resultc == 10)
			{outputCTF.setText("Your staffing is sufficient");}

			else
			{
			throw new TheException(); //calls
            		}
	}
}
	catch (TheException e)
        {
            System.out.println("Illegal input caught, please enter a number");
        }

        System.out.println();
        System.out.println("Program Ending!");
	}
	}
}

	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();
	}
}
Attached Images
File Type: jpg MyError.jpg (35.3 KB, 8 views)
kewlgeye is offline   Reply With Quote
Old Apr 30th, 2008, 4:16 PM   #2
Ezzaral
Newbie
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0 Ezzaral is on a distinguished road
Re: Java Error Messages

ExitButtonHandler and main() are defined outside of your class body. Check your braces. You also have a missing brace on this if() block and no block at all on the (resultb < 8) statement
	if (resultb > 8)
	{resultb = resultb - 8;
. Attention to indentation and blocking standards will go a long way towards preventing these kinds of errors
	if (resultb > 8){
             resultb = resultb - 8;
             outputBTF.setText("You are overstaffed by " + resultb);
        }
	if (resultb < 8){
             resultb = 8 - resultb;
             outputBTF.setText("You are understaffed by " + resultb);
        }
	if (resultb == 8) {
            outputBTF.setText("Your staffing is sufficient");
        }
Ezzaral is offline   Reply With Quote
Old May 1st, 2008, 1:51 PM   #3
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 53
Rep Power: 1 kewlgeye is on a distinguished road
Re: Java Error Messages

Ok, I fixed my brackets, and also had to give the event the ev name instead of"e" because it was already being used. I imported the import java.io.* statement and added the class extends section of the code.

I still have an error, when I enter the numbers in. I receive the catch message, and I shouldn't because I am entering a number. Whats wrong? I attached a picture. I am happy though that I can at least see my program now. Have a look please.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

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);}
			if (resulta < 5)
			{resulta = 5 - resulta;
			outputATF.setText("You are understaffed by " + resulta);}
			if (resulta == 5)
			{outputATF.setText("Your staffing is sufficient");}
			
			
			if (resultb > 8){
			resultb = resultb - 8;
			outputBTF.setText("You are overstaffed by " + resultb);
			}
			if (resultb < 8){
			resultb = 8 - resultb;
			outputBTF.setText("You are understaffed by " + resultb);
			}
			if (resultb == 8){
			outputBTF.setText("Your staffing is sufficient");
			}
			
						

			if (resultc > 10){
			resultc = resultc - 10;
			outputCTF.setText("You are overstaffed by " + resultc);
			}
			if (resultc < 10){
			resultc = 10 - resultc;
			outputCTF.setText("You are understaffed by " + resultc);
			}
			if (resultc == 10){
			outputCTF.setText("Your staffing is sufficient");
			}

			else
			{
			throw new TheException(); //calls
            		}
	}
			catch (TheException ev)
        		{
            		outputATF.setText("" +ev + " You must enter a number");
       			}

		}
	}


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();
	}
}

attached picture
Attached Images
File Type: jpg myError.jpg (44.0 KB, 4 views)
kewlgeye is offline   Reply With Quote
Old May 1st, 2008, 2:38 PM   #4
Ezzaral
Newbie
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0 Ezzaral is on a distinguished road
Re: Java Error Messages

This section is going to throw your exception if resultc is not equal to 10. I doubt that is the intent
			if (resultc == 10){
			outputCTF.setText("Your staffing is sufficient");
			}

			else
			{
			throw new TheException(); //calls
            		}
Ezzaral is offline   Reply With Quote
Old May 1st, 2008, 5:41 PM   #5
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 53
Rep Power: 1 kewlgeye is on a distinguished road
Re: Java Error Messages

Quote:
Originally Posted by Ezzaral View Post
This section is going to throw your exception if resultc is not equal to 10. I doubt that is the intent
			if (resultc == 10){
			outputCTF.setText("Your staffing is sufficient");
			}

			else
			{
			throw new TheException(); //calls
            		}

This isn't my intent. What I am trying to do is perform the calculations based on my if statements, and if anything else is entered like a ., a letter, or anything besides a number, it should be caught and display the sentence in the outputbox "Illegal entry, please enter a number"

Any ideas?
kewlgeye is offline   Reply With Quote
Old May 2nd, 2008, 1:48 AM   #6
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: Java Error Messages

instead of using all if statements you use elseif?
and then leave your else statement at the end.

Chris
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old May 2nd, 2008, 8:39 AM   #7
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 53
Rep Power: 1 kewlgeye is on a distinguished road
Re: Java Error Messages

I figured it out.

I needed to use the NumberFormatException and delete the extends section and also parse within the try statement to have something to try with. 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);
       			}
kewlgeye is offline   Reply With Quote
Old May 2nd, 2008, 1:49 PM   #8
Ezzaral
Newbie
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0 Ezzaral is on a distinguished road
Re: Java Error Messages

Your try-catch on NumberFormatException really only needs to be around this portion
			resulta = Integer.parseInt(resultATF.getText());
			resultb = Integer.parseInt(resultBTF.getText());
			resultc = Integer.parseInt(resultCTF.getText());
because that is where the string values are converted to ints. The rest of the code merely operates on those int values and really has no bearing on NumberFormatException. It certainly doesn't need to throw one explicitly.
Ezzaral is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 10:58 AM
Special browser in Java (Project) stalefish Java 3 Feb 9th, 2008 4:22 PM
First Java Program duale2005 Java 3 May 22nd, 2006 5:17 PM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 7:25 PM
Begin my first lesson to learn Java satimis Java 7 Mar 3rd, 2005 2:45 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC