Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: For loops

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default For loops

    I can't figure out what I am missing, I think I need a break somewhere inside the for loops but I don't know exactly where I would put it, because once it gets to the first for loop it just keep looping and won't continue past it.

    import java.util.Scanner;
     
    public class ********CalculateGrades {
    	public static void main(String[] args) {
     
    		Scanner sc = new Scanner(System.in);
     
    		final int Assignments = 6;
    		final int Tests = 2;
    		double AssignmentAvg;
    		double TestAvg;
    		int Final;
    		int Lab;
    		double Avg;
     
    		int[] AGrade = new int[Assignments];
    		int[] TGrade = new int[Tests];
     
    		System.out.println("Would you like to calculate your grades now?(y/n)");
    		String Input = sc.nextLine();
     
    		while(Input.equals("y") || Input.equals("Y")) {
     
    			System.out.println("Enter your" + Assignments + " assignment grades.");
     
    			for(int index = 0; index < Assignments; index++) {
    				System.out.print("Assignment #" + (index + 1) + ": ");
    				AGrade[index] = sc.nextInt();
     
    				double lowest = AGrade[0];
    				if(AGrade[index] < lowest) {
    					lowest = AGrade[index];
    				}
    				AssignmentAvg = (AGrade[index] - lowest) / Assignments;
    			}
    			System.out.println("Enter your" + Tests + " test grades.");
     
    			for(int index = 0; index < Tests; index++) {
    				System.out.print("Test #" + (index + 1) + ": ");
    				TGrade[index] = sc.nextInt();
    				TestAvg = TGrade[index] / Tests;
    			}
     
    			System.out.println("Enter your lab grade");
    			Lab = sc.nextInt();
     
    			System.out.println("Enter your final grade");
    			Final = sc.nextInt();
     
    			Avg = (AssignmentAvg * .25) + (TestAvg * .30) + (Lab * .25) + (Final * .20);
     
    			if(Avg >= 90) {
    				System.out.println("Your grade is an A, and your average is " + Avg + ".");
    			}else if(Avg >= 80) {
    				System.out.println("Your grade is a B, and your average is " + Avg + ".");
    			}else if(Avg >= 70) {
    				System.out.println("Your grade is a C, and your average is " + Avg + ".");
    			}else if(Avg >= 60) {
    				System.out.println("Your grade is a D, and your average is " + Avg + ".");
    			}else if(Avg < 60) {
    				System.out.println("Your grade is an F, and your average is " + Avg + ".");
    			}
     
    			System.out.println("Do you want to calculate the grade of another student?");
    			Input = sc.nextLine();
    		}
    	System.out.println("Thank you for using my Average Calculator, Bye bye!!");
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: For loops

    I have moved your post to a more appropriate category.

    Please explain your problem more. A for loop is a for loop, so it should end unless something from within prevents this. For example, you ask for user input - do you prompt the user for input? Add some more println's in there to determine where the program hangs.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: For loops

    What it is doing is going through the first for loop, asking for the grade of each assignment, but then when it gets to the 6th assignment it won't continue on with the code, it just goes back to the first assignment.

    Edit:/

    Actually I take that back it is not even doing that, I think the reason it was doing that is because the old .class file was from when I compiled it earlier in the code with just the first for statement, I am just getting 2 errors now and I have no clue why and when I try to run it it says cannot find the class file.

    C:\Users\Joseph Chastain\Desktop\CS Class\Assignments\ChastainJosephCalculateGrades.ja va:58: error: variable AssignmentAvg might not have been initialized
    Avg = (AssignmentAvg * .25) + (TestAvg * .30) + (Lab * .25) + (Final * .20);
    ^
    C:\Users\Joseph Chastain\Desktop\CS Class\Assignments\ChastainJosephCalculateGrades.ja va:58: error: variable TestAvg might not have been initialized
    Avg = (AssignmentAvg * .25) + (TestAvg * .30) + (Lab * .25) + (Final * .20);
    ^
    2 errors
    Last edited by JCTexas; September 21st, 2011 at 03:37 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: For loops

    am just getting 2 errors now and I have no clue why and when I try to run it it says cannot find the class file.
    The description of those errors says it all - one of your variables may not have been initialized. Quick fix is to initialize this variable to a particular value (for example 0)

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: For loops

    See http://www.javaprogrammingforums.com...rror-code.html

Similar Threads

  1. help with loops
    By kidza12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2011, 11:42 PM
  2. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM
  3. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM
  4. How to Use different Java loops
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 03:10 AM
  5. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM