I would love if someone had a few minutes to help me in my programming, I usually am alone doing this trying to learn and all, which entails a bunch of guess and checking with no real pin point of where I went wrong.

http://pastebin.com/pE0giYq1

This is a script/program done on textpad, for getting an average for testgrades. You are prompted to enter a total number of grades, then enter each score. It will then calculate an average and match that average up with a letter grade to display. I have it compiling with no errors, but it will not display anything back. I was told I have to call a method after it calculates the average but I'm not quite sure what that is, I thought I did with the else/else if check method going on in there. I think I have something mislabeled in there or not supposed to be a double etc.

anyone that has a few minutes can you explain in the beginner terms how I went wrong or how I can fix it on my own. Thanks in advance. I really want to learn, just a lot of the guides I've looked up are talking about things in ways I guess I haven't learned throughly.

Recommended Answers

All 12 Replies

Like I stated if you are having problems with the grade not displaying, then I gave you a solution on your yahoo answers post. :)

Yeah I still don't quite understand so I took your advice and came here, I didn't want to pester you with more clarification since I was on the understanding "public void checkgrade()" was starting/calling it. And I'm still trying to figure it out what you mean and where this is messed up.

And people in my experience get pretty mad when you keep asking for further explanation. and after working a lot on this on my own and trying to teach myself then feeling totally defeated I just didn't want to add insult to injury if you got upset.

I think you just need to be a little more clear in your question. Lets start at the beginning (kind of). A program works in layers. The first method (layer) that gets executed is the main method (public static void main (String[] args)). From there you call other methods which add to the layer. Whenever a method is called the program "breaks" and a note is made of where the code was stopped, and it continues with the method called.

So if I were to call a method called getScores and it would be called from the main method the program stack (where the computer keeps track of what methods were executed) would look like this

getScores
main

and then if get scores called a method lets say update(), then the stack would look like

update
getScores
main

when the method finishes executing, then the program return to where it left off in the previous method. So if update finishes then the stack will return to what it looked like in the first one.

What your program does it that it takes an input from the user for the test results or marks, and it then averages them. You then have another method called checkgrade() that checks which grade the average falls into and then updates the display to show that grade. Your problem was that you were not calling that method, and such the display didn't update. Generally you want your program to have a simple layout (a logical one). So just because you can put the update display in the main method, you may not want to because if they don't enter in test scores, then it would be a waste to update the display (though this really only matters when you are updating the display in a loop). I hope this clarifies. If you would like more information please specify a specific topic that you would like us to clarify.

PS. If you con't call a method then it won't execute. You can have methods in your code that aren't run.

EDIT: I read through your question again, and I may be able to clarify a little bit more. You stated that

I was on the understanding "public void checkgrade()" was starting/calling it

The general structure of a method is the following

visibility returnType name (paramater)

When creating a method it doesn't automatically call itself. It just allows you to call it. So essentially the public void checkgrade is a door into a room containing the code. You woun't be able to see or read the code unless you go to the door and open it. It is the same with the methods. The method allows you to store a group of commands in order to accomplish something. However if you don't call the method (go into the method), then you will not be able to execute that code. That is why we needed to put the checkgrade() in the calculateAverage method. It was to execute the method to update the display.

Hope this clarifies

Okay like for instance another program (linked part below) that did work successfully for me was a similar one, except that it didn't do any calculations it was a get input then check style method. So the "method" in this one is that top line correct? And since it was just doing 1 thing in the entire application I didn't need a 2nd method called into the mix. But this current program I have to not only get the inputs and make the calculation it has to then match it up. So before that section would I need another "public static void (string[] args)" to start/call that 2nd method? Or am I still far off base?

public static void main(String[] args)
    {
        CinemaPrice application = new CinemaPrice();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void checkJButtonActionPerformed(ActionEvent event)
    {

        getVoterAge();
    }

    public void getVoterAge()
    {
        try
        {
            voterAge = Integer.parseInt(enterAgeJTextField.getText());
            checkForZeroInput();
        }
        catch(NumberFormatException exception)
        {
                JOptionPane.showMessageDialog(this,
                "Please enter age of voter!",
                "Number Format Error", JOptionPane.ERROR_MESSAGE );
                enterAgeJTextField.setText("");
                enterAgeJTextField.requestFocusInWindow();
        }

Okay this gets weirder but I guess further verifies that my 'checking/mathing' isn't being called. Because if I make the values curriences for whatever reason instead of regular numbers I get a number displayed which is the correct average, just a money value not a letter grade.

I even tried to set this up a different way where it will show the number average and have a commentary box where it will show the matching letter grade http://pastebin.com/y68J1phP

But I guess I'm still having that method calling issue going on.

I'll explain a little more about methods and now constructors. The main method (public static void main (String[] args)) is a special type of method. There is only one main method in any program (there can be more for debugging purposes, but lets just stick with one). This is the method that java searches for when it starts an application (if you don't include it it throws an error). so you want everything to start from the main method. A constructor is essentially a method that is automatically executed when an object is created (in this case when you create the CinemaPrice object). Here is a sample program

public class Sample{

    // the constructor is challed when I create a new Sample()
    public Sample (){
        runProgram();
    }


    // the main method gets executed first
    public static void main(){
        Sample s = new Sample();//could just be new Sample, but it doesn't matter
        runOnce();
    }

    // runOnce is called once from the main method.
    public void runOnce(){

    }

    public void runProgram(){ // runProgram is called whenever a new Sample object is created

    }
}

so in this case the stack would look like this

main

|
v

Sample
main

|
v

runProgram
Sample
main

|
v

Sample
main

|
v

main

|
v

runOnce
main

|
v

main

|
v

(program exits)

In your example the main method is the first method, but the main, checkJButtonActionPerformed and getVoterAge are all methods. The main method just gets executed first. A program can contain as many methods as you want. For example if you make a simple number guessing game you will probably want to make a method for:

generating a number
resetting all the data
the UI
getting the input
main method - must

Methods are used to break a complicated task into multiple simple ones. for example if you make a store program it would be difficult to make a whole store, but if you break it up so that one method handles inventory, one handles income, one handles ordering, one customer support, etc. the task becomes simpler. After the separate tasks are done, your job is to connect them in an efficient manner so that all the functions work.

Hoipe this clarifies.

So this isn't calling that section into action (code attached); I see the main method for the whole program the section that one I linked previously that's tied in near the exit on close.

I'm understanding a bit what you mean from your diagram, just trying to match this up to what I'm seeing visually on what I have to try and see how this would work. I see what I have that public void checkgrade now that isn't a method, just looks pretty similiar to the void runonce etc which is probably what's confusing me.

public void checkgrade()
    {

    if(grade < 64)
            {
                comentaryJTextField.setText("F");
            }
             else if(grade < 69)
            {
                comentaryJTextField.setText("D");

            }
             else if(grade < 79)

public void checkgrade is a method. it has the visibility (public) the return type (void), name (checkgrade) and the parameters (none). The partial code that you posted is the room with the door and the code inside it. you need to open the door (call the method (checkgrade()) would be calling it) to run the code inside. I probably should have specified that everything in my code is a method. a method can take a variety of different forms.

here are some examples

public void method(){ ...code... }
private int method(double i){ ...code... }
double method (int i){ ...code... }

the look of a method depends on what it needs to accomplish. if you need it to return a value (like an average for example) then it needs a return type. If you want it to only be visible in the class(Java can work with multiple classes interacting. More advanced. Take my word for it) you are working with then the visibility needs to be private. If you need it to do set things like update a display you may or may not need parameters. If you haven't already read this. It may help clarify some things about methods. Again just because you create a method doesn't mean it runs. You need to open the door before you can get inside. The same way you need to call a method before you can execute the code inside.

Would this be anywhere closer to where I need to be? I thought I was recalling that average I just calculated previously, and was on the right track but then I still don't get any output from what I put into the prompts. :/

 public int getgrade() {
            return gradeAccumulator / numberOfScores;
    }

    public void checkgrade()
    {

    if(grade < 64)
            {
                comentaryJTextField.setText("F");
            }
             else if(grade < 69)
            {
                comentaryJTextField.setText("D");

            }
             else if(grade < 79)

I fixed the issue where it wouldn't display the numerical value back to me. I have that working at least. I'm still not calling this method right for the letter value to display back in relation to that number.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.awt.Color;

public class GradeCalculator extends JFrame
{

    JLabel gradeJLabel;
    JTextField gradeJTextField;

    JLabel clickstartJLabel;

    JButton enterJButton;
    JButton clearJButton;
    JButton closeJButton;

    JTextField comentaryJTextField;


    String testscores;
    int numberOfScores;
    double priceValues;
    int grade;
    int counter;
    int gradeAccumulator;


    public GradeCalculator()
    {
            createUserInterface();
    }

    public void createUserInterface()
    {
        Container contentPane = getContentPane();
        contentPane.setBackground(Color.WHITE);
        contentPane.setLayout(null);

        gradeJLabel = new JLabel();
        gradeJLabel.setBounds(50, 50, 150, 20);
        gradeJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        gradeJLabel.setText("Grade:");
        gradeJLabel.setForeground(Color.BLACK);
        gradeJLabel.setHorizontalAlignment(JLabel.CENTER);
        contentPane.add(gradeJLabel);

        gradeJTextField = new JTextField();
        gradeJTextField.setBounds(200, 50, 50, 20);
        gradeJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        gradeJTextField.setHorizontalAlignment(JTextField.CENTER);
        gradeJTextField.setForeground(Color.BLACK);
        gradeJTextField.setBackground(Color.WHITE);
        gradeJTextField.setEditable(false);
        contentPane.add(gradeJTextField);

        comentaryJTextField = new JTextField();
        comentaryJTextField.setBounds(200, 100, 50, 20);
        comentaryJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        comentaryJTextField.setHorizontalAlignment(JTextField.CENTER);
        comentaryJTextField.setForeground(Color.BLACK);
        comentaryJTextField.setBackground(Color.WHITE);
        comentaryJTextField.setEditable(false);
        contentPane.add(comentaryJTextField);

        clickstartJLabel = new JLabel();
        clickstartJLabel.setBounds(100, 200, 150, 20);
        clickstartJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        clickstartJLabel.setText("Click Start to Begin!");
        clickstartJLabel.setForeground(Color.BLACK);
        clickstartJLabel.setHorizontalAlignment(JLabel.CENTER);
        contentPane.add(clickstartJLabel);

        enterJButton = new JButton();
        enterJButton.setBounds(20, 300, 100, 20);
        enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
        enterJButton.setText("Enter");
        enterJButton.setForeground(Color.BLACK);
        enterJButton.setBackground(Color.WHITE);
        contentPane.add(enterJButton);
        enterJButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    enterJButtonActionPerformed(event);
                }
            }
        );

        clearJButton = new JButton();
        clearJButton.setBounds(140, 300, 100, 20);
        clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
        clearJButton.setText("Clear");
        clearJButton.setForeground(Color.BLACK);
        clearJButton.setBackground(Color.WHITE);
        contentPane.add(clearJButton);
        clearJButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    clearJButtonActionPerformed(event);
                }
            }
        );

        closeJButton = new JButton();
        closeJButton.setBounds(260, 300, 100, 20);
        closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
        closeJButton.setText("Close");
        closeJButton.setForeground(Color.BLACK);
        closeJButton.setBackground(Color.WHITE);
        contentPane.add(closeJButton);
        closeJButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    closeJButtonActionPerformed(event);
                }
            }
        );


        setTitle("Grade Calculator");
        setSize(400, 400);
        setVisible(true);
    }

    /* main method */
    public static void main(String[] args)
    {
            GradeCalculator application = new GradeCalculator();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void enterJButtonActionPerformed(ActionEvent event)
    {
        getnumberoftests();
    }

    public void getnumberoftests()
        {
            testscores = JOptionPane.showInputDialog("Enter Number of Grades");
            numberOfScores = Integer.parseInt(testscores);
            gettestscores();
    }

    public void gettestscores()
    {
        for(counter = 0; counter < numberOfScores; counter++)
        {
            testscores = JOptionPane.showInputDialog("Enter Scores");
            priceValues = Integer.parseInt(testscores);
            gradeAccumulator += priceValues;
        }

        calculateAverage();
    }

    public void calculateAverage()
    {
                grade = gradeAccumulator / numberOfScores;
                gradeJTextField.setText("" + grade);

    }
    public int getgrade() {
                return gradeAccumulator / numberOfScores;
    }

    public void checkgrade()
        {

        if(grade < 64)
                {
                    comentaryJTextField.setText("F");
                }
                 else if(grade < 69)
                {
                    comentaryJTextField.setText("D");

                }
                 else if(grade < 79)

                {
                    comentaryJTextField.setText("C");

                }
                else if(grade < 89)

                {
                    comentaryJTextField.setText("B");

                }

                else {

                    comentaryJTextField.setText("A");

                }

        }



    public void clearJButtonActionPerformed(ActionEvent event)
    {
        gradeJTextField.setText("");
    }

    public void closeJButtonActionPerformed(ActionEvent event)
    {
        GradeCalculator.this.dispose();
    }
}

Done. and works. After all that I was missing sigh. lol. Thanks for your help. Enjoy your holiday week!

                checkgrade();

Glad I could help. Just remember that in order for a method to execute you need to call it first.

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.