Results 1 to 3 of 3
  1. #1
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default For Loop NOT Incrementing...

    I know this is a really stupid, mundane thing, but for some reason I can't to get anything within the for loop to increment.

    Here is he code:
    Java Code:
    import javax.swing.JOptionPane;
    
    public class Grades {
    	public static void main(String[] args) {
    		String input = JOptionPane.showInputDialog("Enter the number of students:");
    		int students = Integer.parseInt(input);
    		int[] numStudents = new int[students];
    
    		for(int i = 0; i < numStudents.length; i++) {
    			String inputGrade = JOptionPane.showInputDialog("Enter the score of student " + (numStudents[i] + 1) + ":");
    			int grade = Integer.parseInt(inputGrade);
    			int best = 100;
    			
    			if(grade >= best - 10) {
    				System.out.println("Student " + (numStudents[i] + 1) + " score is " + grade + " and grade is A");
    			} else if(grade >= best - 20) {
    				System.out.println("Student " + (numStudents[i] + 1) + " score is " + grade + " and grade is B");
    			} else if(grade >= best - 30) {
    				System.out.println("Student " + (numStudents[i] + 1) + " score is " + grade + " and grade is C");
    			} else if(grade >= best - 40) {
    				System.out.println("Student " + (numStudents[i] + 1) + " score is " + grade + " and grade is D");
    			} else {
    				System.out.println("Student " + (numStudents[i] + 1) + " score is " + grade + " and grade is F");
    			}
    		}
    	}
    }
    Example output:
    If I was to enter 3 student and their grades, for all it says, "Student 1 score is..." I have no idea why it won't increment.

    Any suggestions? Thanks in advance for the help...

  2. #2
    vivekanand30 is offline *****
    Join Date
    Sep 2011
    Location
    Mumbai
    Posts
    8
    Rep Power
    0

    Default Re: For Loop NOT Incrementing...

    Look for this line

    String inputGrade = JOptionPane.showInputDialog("Enter the score of student " + (numStudents[i] + 1) + ":");

    Here you are using "numStudents[i] + 1" as default value in array would be 0 so it would always come as 1 instead of this use "i+1"

  3. #3
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: For Loop NOT Incrementing...

    Man do I feel stupid. It worked. Thanks for your help.

Similar Threads

  1. Incrementing by more than 1
    By davidq23 in forum New To Java
    Replies: 2
    Last Post: 07-27-2011, 01:12 AM
  2. Help with incrementing numbers in a GUI
    By jdg951 in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 03:23 AM
  3. incrementing array elements
    By porchrat in forum New To Java
    Replies: 2
    Last Post: 10-06-2010, 02:37 PM
  4. Incrementing and loops
    By Rubidoux in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 02:04 PM
  5. Incrementing jbutton in for loop
    By RobertF in forum New To Java
    Replies: 3
    Last Post: 03-12-2009, 11:41 AM

Posting Permissions

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