Hi guys, i dont think im using the boolean and break correctly i need it to see the tutormin>=240 and then throw the error and stop without exiting the program. can someone take a look and help me edit it

import java.awt.*; //imports all Java.awt
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*; // Imports all swing 
/*
Author James Perkins
* Intro to programming
* Task 1
* 
*/

class Tuthelp extends JFrame {

    private JPanel panel;
        private JLabel TutorTimeLabel; // Time label
    private JTextField timeField; // Field for time info
    private JLabel TutorWageLabel; //Label for Tutor wages
    private JTextField wageField; // Field to enter info for Wage
    private JTextArea textArea; // Text where all GUI info will show after report is run 
    private JButton enterButton; //Enter Button in GUI
    private JButton runReportsButton; // Run report in GUI
    private JButton quitButton; // Exit program button
    final int WINDOW_WIDTH = 600; // Window Width
    final int WINDOW_HEIGHT = 600; // Window height
    double [][] studentData = new double [3][3]; // 
        int nextStudent=0;

        public Tuthelp()
    {
    setTitle("Tutor Earnings "); // GUI title
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //sets the size of the window.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        buildPanel();
        add(panel);
        setVisible(true);
}


        public void buildPanel(){
  //* This part Creates the GUI using labels and Panels described below/*

        TutorTimeLabel = new JLabel("Tutor Time");
    timeField = new JTextField(10);
    TutorWageLabel = new JLabel ("Tutor Earnings");
    wageField = new JTextField(10);
    textArea = new JTextArea(25,50);
    enterButton = new JButton("Enter");
    enterButton.addActionListener(new Tuthelp.enterButtonListener());
    runReportsButton = new JButton("Run Reports");
    runReportsButton.addActionListener(new Tuthelp.runReportsButtonListener());
    quitButton = new JButton("Quit");
    quitButton.addActionListener(new quitButtonListener());
    panel = new JPanel();
    panel.add(TutorTimeLabel);
    panel.add(timeField);
    panel.add(TutorWageLabel);
    panel.add(wageField);
    panel.add(textArea);
    panel.add(enterButton);
    panel.add(runReportsButton);
    panel.add(quitButton); 
    }

    private class enterButtonListener implements ActionListener // Actions performed when enter is hit are below
    {

    public void actionPerformed(ActionEvent e)

        {      
        boolean error =false;
        double minsIdx = Double.parseDouble(timeField.getText());
        double wagesIdx= Double.parseDouble(wageField.getText());     

        studentData[nextStudent][0] = minsIdx;        // Minutes entered
        studentData[nextStudent][1] = wagesIdx;       // Wages Entered 

        nextStudent++; //  Ready for the next Student entry.

        // the following Code below is an IF statement to make sure the user enters the correct information.
        time:
        if (minsIdx <=0){
    JOptionPane.showMessageDialog(null, "Please enter a time larger than 0", "Error",
    JOptionPane.ERROR_MESSAGE);
                }
        else if (minsIdx >= 240)
        {    
    JOptionPane.showMessageDialog(null, "You have Tutored this Person too long", "Error",
    JOptionPane.ERROR_MESSAGE);
        error = true;
        break time;
        }else{
    if(wagesIdx <= 0) // They must get paid so error if wage is zero.
    {
    JOptionPane.showMessageDialog(null, "Please enter a wage", "Error", 
    JOptionPane.ERROR_MESSAGE);
        }

        timeField.setText("");
        wageField.setText("");
    wageField.requestFocus();
        timeField.requestFocus();

    }
    }
    }
        private class quitButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0); // When the button is pressed the program will close.
            }
        }
    private class runReportsButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
        int cols=2;
        int rows=studentData[0].length;
        double average = 0.00d; // Average time, with two decimal places
    double totalMin = 0.00d; // total mintues work with two decimal place.
    final double MIN_WAGE = 7.25;// This is the minimum wage interger.
    double totalWage = 0.00d; // Total wages, with a two decimal places.
    String wage = "";

    wage+=
"****************************************************\n\n";
        wage+= "Tutoring Earnings Data \n\n"; // This is a heading of the first part of the report
        wage+= "\n"; 
        wage+= "Mintues       Earnings\n"; // this will show the amount they entered.

        for(int i = 0; i< rows;i++){
            for(int j =0; j <cols; j++ ){
               wage += studentData[i][j];
               wage += "              ";

               if (j == 0) {
                   totalMin += studentData[i][j];
               }
               else if (j == 1){
                   totalWage += studentData[i][j];
            }
        }
            wage+= "\n";
        }

        if (studentData[0].length >0 ) {
            average = totalWage / studentData[0].length; // Shows how we work out the average. total wage divided by the amount of time.
        }

        wage +=
"************************************************************************\n\n";
        wage += "Reports of your wages to Date:\n"; // Displays the top of the report
        wage += "_____________________________\n\n"; // Displays a line to keep the gui neet
        wage += "Total Mintues spent tutoring = " + totalMin + "\n"; // Shows total of all mintues entered.
        wage += "Total Earnings = $" + totalWage + "\n";// Shows total of earnings
        wage += "Average Per Hour = $ " + average + "\n"; // Divides the total wage by the mintues entered.
        wage += "Minimum wage = $" + MIN_WAGE + "per hour" + "\n";// The State minimum wage

        wage += "Your average wage are ";
        // The Follow IF statement will decide if the average wage entered is, below, average, or above.
        if (average < MIN_WAGE){
            wage +=" Below Average";
            } else if (average >= MIN_WAGE && average <= MIN_WAGE * 2.00) {
                    wage += "Average";
            } else if (average > MIN_WAGE *2.00){
                wage += "Above Average";
            }
        textArea.setText(wage); // where the report is sent to.
        }}
        public static void main (String[] args)
        {
            Tuthelp th = new Tuthelp();
        }
}

Thanks in advance

Recommended Answers

All 14 Replies

Where in the posted code is the problem? I can not find: tutormin in the posted code.

 private class enterButtonListener implements ActionListener // Actions performed when enter is hit are below
    {
    public void actionPerformed(ActionEvent e)
        {      
        boolean error =false;
        double minsIdx = Double.parseDouble(timeField.getText());
        double wagesIdx= Double.parseDouble(wageField.getText());     
        studentData[nextStudent][0] = minsIdx;        // Minutes entered
        studentData[nextStudent][1] = wagesIdx;       // Wages Entered 
        nextStudent++; //  Ready for the next Student entry.
        // the following Code below is an IF statement to make sure the user enters the correct information.
        time:
        if (minsIdx <=0){
    JOptionPane.showMessageDialog(null, "Please enter a time larger than 0", "Error",
    JOptionPane.ERROR_MESSAGE);
                }
        else if (minsIdx >= 240)
        {    
    JOptionPane.showMessageDialog(null, "You have Tutored this Person too long", "Error",
    JOptionPane.ERROR_MESSAGE);
        error = true;
        break time;
        }else{
    if(wagesIdx <= 0) // They must get paid so error if wage is zero.
    {
    JOptionPane.showMessageDialog(null, "Please enter a wage", "Error", 
    JOptionPane.ERROR_MESSAGE);
        }
        timeField.setText("");
        wageField.setText("");
    wageField.requestFocus();
        timeField.requestFocus();
    }
    }
    }

here sorry its minsIdx

then throw the error and stop

Where does the code throw an exception?

Can you explain what you want to code to do when it finds the invalid value?

When it finds the invalid value on line 17 it shows the You have Tutored this Person too long", "Error", it should stop it from entering the information so they can re-enter the info correctly.

The use of "throws" is misleading. throw is a java statement used with exceptions. A better description would be to say that the program displays an error message.

The break statement is used to exit a loop. There is no loop where you have coded it.
Where do you want the program's execution flow to go when the error is detected?

Yes, I caught i was using the word throw sorry. I would like the program to essiently give them the option to re-enter the info so it would have to start the enterButton action again, basicly all i want the program to do is not let them enter the info, if they enter 240 as the minutes it currently will let the array accept it and calculate it in the report.

You should validate the data BEFORE putting it in the array.

So i tried using a boolean to validate the input, and use a return; but that still causes the array to recieve the date

Did you follow Norm's last post?
Maybe its time to post the current version of the code so we can see what you've done.

new code looks like this.

import java.awt.*; //imports all Java.awt
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*; // Imports all swing 
/*
Author James Perkins
* Intro to programming
* Task 1
* 
*/

class Tuthelp extends JFrame {

    private JPanel panel;
        private JLabel TutorTimeLabel; // Time label
    private JTextField timeField; // Field for time info
    private JLabel TutorWageLabel; //Label for Tutor wages
    private JTextField wageField; // Field to enter info for Wage
    private JTextArea textArea; // Text where all GUI info will show after report is run 
    private JButton enterButton; //Enter Button in GUI
    private JButton runReportsButton; // Run report in GUI
    private JButton quitButton; // Exit program button
    final int WINDOW_WIDTH = 600; // Window Width
    final int WINDOW_HEIGHT = 600; // Window height
    double [][] studentData = new double [3][3]; // 
        int nextStudent=0;

        public Tuthelp()
    {
    setTitle("Tutor Earnings "); // GUI title
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //sets the size of the window.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        buildPanel();
        add(panel);
        setVisible(true);
}


        public void buildPanel(){
  //* This part Creates the GUI using labels and Panels described below/*

        TutorTimeLabel = new JLabel("Tutor Time");
    timeField = new JTextField(10);
    TutorWageLabel = new JLabel ("Tutor Earnings");
    wageField = new JTextField(10);
    textArea = new JTextArea(25,50);
    enterButton = new JButton("Enter");
    enterButton.addActionListener(new Tuthelp.enterButtonListener());
    runReportsButton = new JButton("Run Reports");
    runReportsButton.addActionListener(new Tuthelp.runReportsButtonListener());
    quitButton = new JButton("Quit");
    quitButton.addActionListener(new quitButtonListener());
    panel = new JPanel();
    panel.add(TutorTimeLabel);
    panel.add(timeField);
    panel.add(TutorWageLabel);
    panel.add(wageField);
    panel.add(textArea);
    panel.add(enterButton);
    panel.add(runReportsButton);
    panel.add(quitButton); 
    }

    private class enterButtonListener implements ActionListener // Actions performed when enter is hit are below
    {

    public void actionPerformed(ActionEvent e)

        {      
        boolean error =false;
        double minsIdx = Double.parseDouble(timeField.getText());
        double wagesIdx= Double.parseDouble(wageField.getText());     

        studentData[nextStudent][0] = minsIdx;        // Minutes entered
        studentData[nextStudent][1] = wagesIdx;       // Wages Entered 

        nextStudent++; //  Ready for the next Student entry.

        // the following Code below is an IF statement to make sure the user enters the correct information.
        time:
        if (minsIdx <=0){
    JOptionPane.showMessageDialog(null, "Please enter a time larger than 0", "Error",
    JOptionPane.ERROR_MESSAGE);
                }
        else if (minsIdx >= 240)
        {    
    JOptionPane.showMessageDialog(null, "You have Tutored this Person too long", "Error",
    JOptionPane.ERROR_MESSAGE);
        error = true;
        break time;
        }else{
    if(wagesIdx <= 0) // They must get paid so error if wage is zero.
    {
    JOptionPane.showMessageDialog(null, "Please enter a wage", "Error", 
    JOptionPane.ERROR_MESSAGE);
        }

        timeField.setText("");
        wageField.setText("");
    wageField.requestFocus();
        timeField.requestFocus();

    }
    }
    }
        private class quitButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0); // When the button is pressed the program will close.
            }
        }
    private class runReportsButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
        int cols=2;
        int rows=studentData[0].length;
        double average = 0.00d; // Average time, with two decimal places
    double totalMin = 0.00d; // total mintues work with two decimal place.
    final double MIN_WAGE = 7.25;// This is the minimum wage interger.
    double totalWage = 0.00d; // Total wages, with a two decimal places.
    String wage = "";

    wage+=
"****************************************************\n\n";
        wage+= "Tutoring Earnings Data \n\n"; // This is a heading of the first part of the report
        wage+= "\n"; 
        wage+= "Mintues       Earnings\n"; // this will show the amount they entered.

        for(int i = 0; i< rows;i++){
            for(int j =0; j <cols; j++ ){
               wage += studentData[i][j];
               wage += "              ";

               if (j == 0) {
                   totalMin += studentData[i][j];
               }
               else if (j == 1){
                   totalWage += studentData[i][j];
            }
        }
            wage+= "\n";
        }

        if (studentData[0].length >0 ) {
            average = totalWage / studentData[0].length; // Shows how we work out the average. total wage divided by the amount of time.
        }

        wage +=
"************************************************************************\n\n";
        wage += "Reports of your wages to Date:\n"; // Displays the top of the report
        wage += "_____________________________\n\n"; // Displays a line to keep the gui neet
        wage += "Total Mintues spent tutoring = " + totalMin + "\n"; // Shows total of all mintues entered.
        wage += "Total Earnings = $" + totalWage + "\n";// Shows total of earnings
        wage += "Average Per Hour = $ " + average + "\n"; // Divides the total wage by the mintues entered.
        wage += "Minimum wage = $" + MIN_WAGE + "per hour" + "\n";// The State minimum wage

        wage += "Your average wage are ";
        // The Follow IF statement will decide if the average wage entered is, below, average, or above.
        if (average < MIN_WAGE){
            wage +=" Below Average";
            } else if (average >= MIN_WAGE && average <= MIN_WAGE * 2.00) {
                    wage += "Average";
            } else if (average > MIN_WAGE *2.00){
                wage += "Above Average";
            }
        textArea.setText(wage); // where the report is sent to.
        }}
        public static void main (String[] args)
        {
            Tuthelp th = new Tuthelp();
        }
}

break is normally used for loops etc. To stop any further processing in a method, just call return; at the appropriate point.

Yes, you did ignore Norm's post "You should validate the data BEFORE putting it in the array." You put the data in the array on lines 74,75 then validate from lines 80 onwards. Norm knows what he is talking about.

I guess i was looking at the wrong line's when he said to validate the array.. so i should put the validation before 80 and then use return; correct

Almost right - the validation needs to go before lines 74/75 where you put the data in the arrays.

Now i see what Norm was talking about, I have it now
Thank you very much both of you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.