For loop trouble

• 03-27-2011, 01:17 AM
2wyked
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.
• 03-27-2011, 01:58 AM
Fubarable
• 03-27-2011, 01:58 AM
pbrockway2
Quote:

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.
• 03-27-2011, 03:09 AM
2wyked
Code:

```import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GradeCalculator extends JFrame {         //declarations                         Color black = new Color(0, 0, 0);         Color white = new Color(255, 255, 255);                 JLabel enterGradeJLabel;         JTextField enterGradeJTextField;                 JLabel enterCommandJLabel;                 int numberScores;         int scoreAmount;         int accumulatorVariable;         String inputNumber;         String scoreNumber;                 JButton enterJButton;         JButton clearJButton;                         //Variables need to be declared                         public GradeCalculator()                 {                                 createUserInterface();                 }                                 public void createUserInterface()                 {                 Container contentPane = getContentPane();                 contentPane.setBackground(white);                 contentPane.setLayout(null);                                 enterGradeJLabel = new JLabel();                 enterGradeJLabel.setBounds(100, 75, 100, 20);                 enterGradeJLabel.setFont(new Font("Default", Font.PLAIN, 12));                 enterGradeJLabel.setText("Grade");                 enterGradeJLabel.setForeground(black);                 enterGradeJLabel.setHorizontalAlignment(JLabel.LEFT);                 contentPane.add(enterGradeJLabel);                                 enterGradeJTextField = new JTextField();                 enterGradeJTextField.setBounds(210, 75, 50, 20);                 enterGradeJTextField.setFont(new Font("Default", Font.PLAIN, 12));                 enterGradeJTextField.setHorizontalAlignment(JTextField.CENTER);                 enterGradeJTextField.setForeground(Color.black);                 enterGradeJTextField.setBackground(Color.white);                 enterGradeJTextField.setEditable(false);                 contentPane.add(enterGradeJTextField);                                 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);                 contentPane.add(enterCommandJLabel);                                 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);                 contentPane.add(enterJButton);                 enterJButton.addActionListener(                                                 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);                 contentPane.add(clearJButton);                 clearJButton.addActionListener(                                         new ActionListener()                         {                                 public void actionPerformed(ActionEvent event)                                 {                                         //clearJButtonActionPerformed(event);                                  }                         }                 );                                 setTitle("Grade Calculator");                 setSize(400, 400);                 setVisible(true);                 }                 public static void main(String[] args)         {                         GradeCalculator application = new GradeCalculator();                         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.
• 03-27-2011, 03:32 AM
pbrockway2
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]     {         // your code here!     } }```

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.
• 03-27-2011, 04:16 AM
2wyked
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.
• 03-27-2011, 04:22 AM
pbrockway2
Quote:

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.)
• 03-27-2011, 06:50 PM
Solarsonic
Quote:

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)
Code:

```String[] input = new String[numberOfScores]; for(int i=0; i<numberOfScores; i++) input[i] = JOptionPane.showInputDialog(parameters);```