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 4 of 4

Thread: Error in code

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

    Default Error in code

    Ok I don't know if I can delete my old post but http://www.javaprogrammingforums.com...050-loops.html is useless I figured out that it wasn't a problem with my loop

    I wrote this code twice once with arrays and once by doing a bunch of if statements because I thought maybe the array was causing the problem.

    BUT, the problem I am getting with both examples is that I can't get the variable that I calculated inside of the if statements out or out of the for loop, and idk how to do this I have been looking at my book and googling just to see if I could find a way, but I can't so I come to you.


    If Statement's
    import java.util.Scanner;
     
    public class ChastainJosephCalculateGrades1 {
    	public static void main(String[] args) {
     
    		Scanner sc = new Scanner(System.in);
     
    		double Assignment1;
    		double Assignment2;
    		double Assignment3;
    		double Assignment4;
    		double Assignment5;
    		double Assignment6;
    		double AssignmentAvg;
    		double Test1;
    		double Test2;
    		double TestAvg;
    		double Final;
    		double Lab;
    		double Avg;
    		String Input;
     
     
    		System.out.println("Would you like to calculate your grade?(y/n)");
    		Input = sc.nextLine();
     
    		while(Input.equals("y") || Input.equals("Y")) {
     
    			System.out.print("What was your first assignment grade?");
    			Assignment1 = sc.nextDouble();
     
    			System.out.print("What was your second assignment grade?");
    			Assignment2 = sc.nextDouble();
     
    			System.out.print("What was your third assignment grade?");
    			Assignment3 = sc.nextDouble();
     
    			System.out.print("What was your fourth assignment grade?");
    			Assignment4 = sc.nextDouble();
     
    			System.out.print("What was your fifth assignment grade?");
    			Assignment5 = sc.nextDouble();
     
    			System.out.print("What was your sixth assignment grade?");
    			Assignment6 = sc.nextDouble();
     
    			System.out.print("What was your first test grade?");
    			Test1 = sc.nextDouble();
     
    			System.out.print("What was your second test grade?");
    			Test2 = sc.nextDouble();
     
    			System.out.print("What was your lab grade?");
    			Lab = sc.nextDouble();
     
    			System.out.print("What was your final exam grade?");
    			Final = sc.nextDouble();
     
    			if(Assignment1 < Assignment2 && Assignment1 < Assignment3 && Assignment1 < Assignment4 && Assignment1 < Assignment5 && Assignment1 < Assignment6) {
    				AssignmentAvg = (Assignment2 + Assignment3 + Assignment4 + Assignment5 + Assignment6) / 5;
    			}else if(Assignment2 < Assignment1 && Assignment2 < Assignment3 && Assignment2 < Assignment4 && Assignment2 < Assignment5 && Assignment2 < Assignment6) {
    				AssignmentAvg = (Assignment1 + Assignment3 + Assignment4 + Assignment5 + Assignment6) / 5;
    			}else if(Assignment3 < Assignment2 && Assignment3 < Assignment1 && Assignment3 < Assignment4 && Assignment3 < Assignment5 && Assignment3 < Assignment6) {
    				AssignmentAvg = (Assignment2 + Assignment1 + Assignment4 + Assignment5 + Assignment6) / 5;
    			}else if(Assignment4 < Assignment2 && Assignment4 < Assignment3 && Assignment4 < Assignment1 && Assignment4 < Assignment5 && Assignment4 < Assignment6) {
    				AssignmentAvg = (Assignment2 + Assignment3 + Assignment4 + Assignment5 + Assignment6) / 5;
    			}else if(Assignment5 < Assignment2 && Assignment5 < Assignment3 && Assignment5 < Assignment4 && Assignment5 < Assignment1 && Assignment5 < Assignment6) {
    				AssignmentAvg = (Assignment2 + Assignment3 + Assignment4 + Assignment1 + Assignment6) / 5;
    			}else if(Assignment6 < Assignment2 && Assignment6 < Assignment3 && Assignment6 < Assignment4 && Assignment6 < Assignment5 && Assignment6 < Assignment1) {
    				AssignmentAvg = (Assignment2 + Assignment3 + Assignment4 + Assignment5 + Assignment1) / 5;
    			}
     
    			TestAvg = (Test1 + Test2) / 2;
     
    			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!!.");
    	}
    }

    Error:
    C:\Users\Joseph Chastain\Desktop\CS Class\Assignments\ChastainJosephCalculateGrades1.j ava:83: error: variable AssignmentAvg might not have been initialized
    Avg = (AssignmentAvg * .25) + (TestAvg * .30) + (Lab * .25) + (Final * .20);



    Arrays
    import java.util.Scanner;
     
    public class ChastainJosephCalculateGrades {
    	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!!");
    	}
    }

    Error:
    C:\Users\Joseph Chastain\Desktop\CS Class\Assignments\ChastainJosephCalculateGrades.ja va:59: 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:59: error: variable TestAvg might not have been initialized
    Avg = (AssignmentAvg * .25) + (TestAvg * .30) + (Lab * .25) + (Final * .20);
    Last edited by JCTexas; September 21st, 2011 at 04:34 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Error in code

    All you need to do is initialise the variables when they're first declared, or somewhere in the program that is guaranteed to be executed.
    At the moment, those variables are only being initialised inside if-statements / for loops. The problem is, it's not guaranteed that any of the if conditions will ever be true.

    As I said, you could initialise the variable when they're first declared, or in an else clause, which is always guaranteed to be executed if other conditions aren't met.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    JCTexas (September 21st, 2011)

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

    Default Re: Error in code

    Please don't duplicate questions such as this - I spent my time answering your same question in your other thread only to find I spent my time answering a question that was already answered somewhere else. Best practice is to ask a question once...and if for any reason there is overlap between threads provide links to each thread in both.

  5. The Following User Says Thank You to copeg For This Useful Post:

    JCTexas (September 21st, 2011)

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

    Default Re: Error in code

    Quote Originally Posted by copeg View Post
    Please don't duplicate questions such as this - I spent my time answering your same question in your other thread only to find I spent my time answering a question that was already answered somewhere else. Best practice is to ask a question once...and if for any reason there is overlap between threads provide links to each thread in both.
    I do apologize, I just thought since that was in a sub forum about loops It would be best for me to re ask the question over here.

Similar Threads

  1. Replies: 4
    Last Post: July 25th, 2011, 06:12 PM
  2. Simple code error
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2010, 03:25 PM
  3. [SOLVED] Help with an error in code
    By cis170 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 6th, 2010, 10:21 AM
  4. Error with code
    By JJTierney in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 05:23 PM