1. Member
Join Date
Mar 2011
Posts
10
Rep Power
0

## For loop trouble

Im new to java. I have an assignment where I have to make a grade calculator. When the enter button is clicked a joptionpane appears and asks the user how many grades are to be entered. After clicking ok another joptionpane appears asking for the scores so they can be stored and later averaged. The number of joptionpanes that appear asking for scores depends on the value that was entered into the first joptionpane. I have to create a for loop. I understand the concept of a for loop but I do not know how to correctly code the for loop so that the joptionpane repetitively asks for scores depending on the value entered in the first joptionpane. If anybody could point me in the right direction it would be greatly appreaciated.

2. Please show us what you've tried so far so we can better understand your problem and better be able to help you.

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
I understand the concept of a for loop but I do not know how to correctly code the for loop so that...

The for loop has three bit to it, right? In this case they might be the starting value of some counter, the stopping condition and a bit that increments the counter. Inside the loop goes the stuff you want to happen repetitively.

To get anywhere you are going to have to take the first steps! Write some code and, however badly it turns out, you will have something to ask about.

You don't have to get all the way to a solution in one go. Just a loop which asks the user to enter a grade the right number of times will be a start. (You might also want to print that grade to test that everything is going well).

If your code doesn't do what you expect, post the code and say what you expected as well as what it actually does.

4. Member
Join Date
Mar 2011
Posts
10
Rep Power
0
Java Code:
```import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

{
//declarations

Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);

JLabel enterCommandJLabel;

int numberScores;
int scoreAmount;
int accumulatorVariable;
String inputNumber;
String scoreNumber;

JButton enterJButton;
JButton clearJButton;

//Variables need to be declared

{
createUserInterface();
}

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

enterCommandJLabel = new JLabel();
enterCommandJLabel.setBounds(100, 200, 200, 20);
enterCommandJLabel.setFont(new Font("Default", Font.PLAIN, 12));
enterCommandJLabel.setText("Click Enter To Start!");
enterCommandJLabel.setForeground(Color.black);
enterCommandJLabel.setHorizontalAlignment(JTextField.CENTER);

enterJButton = new JButton();
enterJButton.setBounds(100, 300, 100, 20);
enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);

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

clearJButton = new JButton();
clearJButton.setBounds(210, 300, 100, 20);
clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);

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

setSize(400, 400);
setVisible(true);
}

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

}

public void enterJButtonActionPerformed(ActionEvent event)
{

inputNumber = JOptionPane.showInputDialog("Number of Scores to be Entered?");
numberScores = Integer.parseInt(inputNumber);

scoreNumber = JOptionPane.showInputDialog("Enter a Score");
scoreAmount = Integer.parseInt(scoreNumber);

for(scoreAmount = 1; numberScores <= scoreAmount; accumulatorVariable += scoreAmount);
{}

}

}```

This is what I have so far. I have very bad instruction for my class let alone any at all so please bear with me.
Last edited by Fubarable; 03-27-2011 at 02:10 AM. Reason: code tags added

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Java Code:
```public void enterJButtonActionPerformed(ActionEvent event)
{
inputNumber = JOptionPane.showInputDialog("Number of Scores to be Entered?");
numberScores = Integer.parseInt(inputNumber);

scoreNumber = JOptionPane.showInputDialog("Enter a Score");
scoreAmount = Integer.parseInt(scoreNumber);

for(scoreAmount = 1; numberScores <= scoreAmount; accumulatorVariable += scoreAmount)[b][color=red];[/color][/b]
{
}
}```

The semicolon at the end of the for(...) statement is wrong. But more importantly you haven't written anything in the body of the for loop!

What do you expect to happen repeatedly? Whatever that is, that's what goes into the body of the for loop.

6. Member
Join Date
Mar 2011
Posts
10
Rep Power
0
Well thats my problem I dont know what the code would be for the body. I would like for the 2nd joptionpane(scores) to repeat depending on the value entered into the 1st joption pane (number of scores to be entered), then the scores to be accumulated and averaged. Im pretty confident with if statements but I cant get past the for loop.

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
I would like for the 2nd joptionpane(scores) to repeat depending on the value entered into the 1st joption pane (number of scores to be entered), then ...

So put the second JOptionPane.showInputDialog() inside the body of the for loop if you want it to repeat.

But first check the three bits of the for loop to make sure they do what they should. (See my first response and the fact that the for loop involves what I was referring to as a counter which is another variable having nothing to do with the values the user enters. It is just there so that it can start at zero and keep increasing the right number of times.)

8. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
3
Originally Posted by 2wyked
Im new to java. I have an assignment where I have to make a grade calculator. When the enter button is clicked a joptionpane appears and asks the user how many grades are to be entered. After clicking ok another joptionpane appears asking for the scores so they can be stored and later averaged. The number of joptionpanes that appear asking for scores depends on the value that was entered into the first joptionpane. I have to create a for loop. I understand the concept of a for loop but I do not know how to correctly code the for loop so that the joptionpane repetitively asks for scores depending on the value entered in the first joptionpane. If anybody could point me in the right direction it would be greatly appreaciated.
1. Instead of using an array and requiring the user to input how many grades they are entering (which isn't very convenient at all), use an ArrayList (which holds values without needing to specify a specific number of elements).

2. (Using an array)
Java Code:
```String[] input = new String[numberOfScores];
for(int i=0; i<numberOfScores; i++)
input[i] = JOptionPane.showInputDialog(parameters);```