Results 1 to 8 of 8
  1. #1
    2wyked is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

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

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    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. #4
    2wyked is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Java 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.
    Last edited by Fubarable; 03-27-2011 at 02:10 AM. Reason: code tags added

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    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]
        {
            // 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.

  6. #6
    2wyked is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    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. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    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. #8
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by 2wyked View Post
    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);

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM
  2. Trouble with for loop
    By subiedude101 in forum New To Java
    Replies: 4
    Last Post: 02-13-2011, 10:27 PM
  3. trouble with loop
    By aamster in forum New To Java
    Replies: 10
    Last Post: 10-16-2009, 10:18 PM
  4. [SOLVED] Trouble with this loop!
    By PureAwesomeness in forum New To Java
    Replies: 35
    Last Post: 02-02-2009, 08:04 PM
  5. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 05:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •