I'm completely new to Java, and programming in general, and I'm trying to do something that I thought would be simple but I'm stumped. I probably did something wrong thats obvious to other people but not to me.

I'm trying to display a GPA after you put in the grades, but I have no idea what I did wrong.

import java.util.Scanner;

class grades {
	
	public static void main(String args[]) {
		Scanner myScanner = new Scanner(System.in);
		int grade1;
		int grade2;
		int grade3;
		int grade4;
		int grade5;
		int grade6;
		char reply1;
		char reply2;
		char reply3;
		char reply4;
		char reply5;
		char reply6;
		double GPA;
				
		System.out.print("What is the First period grade? ");
		reply1 = myScanner.findInLine(".").charAt(0);
		
		{grade1 = 4;} if (reply1 == 'A' || reply1 == 'a');
		{grade1 = 3;} if (reply1 == 'B' || reply1 == 'b');
		{grade1 = 2;} if (reply1 == 'C' || reply1 == 'c');
		{grade1 = 1;} if (reply1 == 'D' || reply1 == 'd');
		{grade1 = 0;} if (reply1 == 'F' || reply1 == 'f');
		
		System.out.print("What is the Second period grade? ");
		reply2 = myScanner.findInLine(".").charAt(0);
		
		{grade2 = 4;} if (reply2 == 'A' || reply2 == 'a');
		{grade2 = 3;} if (reply2 == 'B' || reply2 == 'b');
		{grade2 = 2;} if (reply2 == 'C' || reply2 == 'c');
		{grade2 = 1;} if (reply2 == 'D' || reply2 == 'd');
		{grade2 = 0;} if (reply2 == 'F' || reply2 == 'f');
		
		System.out.print("What is the Third period grade? ");
		reply3 = myScanner.findInLine(".").charAt(0);
		
		{grade3 = 4;} if (reply3 == 'A' || reply3 == 'a');
		{grade3 = 3;} if (reply3 == 'B' || reply3 == 'b');
		{grade3 = 2;} if (reply3 == 'C' || reply3 == 'c');
		{grade3 = 1;} if (reply3 == 'D' || reply3 == 'd');
		{grade3 = 0;} if (reply3 == 'F' || reply3 == 'f');
		
		System.out.print("What is the Fourth period grade? ");
		reply4 = myScanner.findInLine(".").charAt(0);
		
		{grade4 = 4;} if (reply4 == 'A' || reply4 == 'a');
		{grade4 = 3;} if (reply4 == 'B' || reply4 == 'b');
		{grade4 = 2;} if (reply4 == 'C' || reply4 == 'c');
		{grade4 = 1;} if (reply4 == 'D' || reply4 == 'd');
		{grade4 = 0;} if (reply4 == 'F' || reply4 == 'f');
		
		System.out.print("What is the Fifth period grade? ");
		reply5 = myScanner.findInLine(".").charAt(0);
		
		{grade5 = 4;} if (reply5 == 'A' || reply5 == 'a');
		{grade5 = 3;} if (reply5 == 'B' || reply5 == 'b');
		{grade5 = 2;} if (reply5 == 'C' || reply5 == 'c');
		{grade5 = 1;} if (reply5 == 'D' || reply5 == 'd');
		{grade5 = 0;} if (reply5 == 'F' || reply5 == 'f');
		
		System.out.print("What is the Sixth period grade? ");
		reply6 = myScanner.findInLine(".").charAt(0);
		
		{grade6 = 4;} if (reply6 == 'A' || reply6 == 'a');
		{grade6 = 3;} if (reply6 == 'B' || reply6 == 'b');
		{grade6 = 2;} if (reply6 == 'C' || reply6 == 'c');
		{grade6 = 1;} if (reply6 == 'D' || reply6 == 'd');
		{grade6 = 0;} if (reply6 == 'F' || reply6 == 'f');
		
		
		GPA = ((grade1 + grade2 + grade3 + grade4 + grade5 + grade6)/6);
		
		System.out.print("Your child's GPA is ");
		System.out.print(GPA);
	}
}

Your syntax for if statements is wrong. The syntax is as follows:

if (someCondition) {
    // do something
}

i tried that, but it didn't work, it kept saying the variables might not have been initialized

i tried that, but it didn't work, it kept saying the variables might not have been initialized

Can you repost your code with the correct syntax?

i think this is what you were saying but, once again, i'm new to this and feel stupid for not knowing what to do

import java.util.Scanner;

class grades {
	
	public static void main(String args[]) {
		Scanner myScanner = new Scanner(System.in);
		int grade1;
		int grade2;
		int grade3;
		int grade4;
		int grade5;
		int grade6;
		char reply1;
		char reply2;
		char reply3;
		char reply4;
		char reply5;
		char reply6;
		double GPA;
				
		System.out.print("What is the First period grade? ");
		reply1 = myScanner.findInLine(".").charAt(0);
		
		 {if (reply1 == 'A' || reply1 == 'a') {
		 	grade1 = 4;
		 	}
		 if (reply1 == 'B' || reply1 == 'b') {
		 	grade1 = 3;
		 	}
		 if (reply1 == 'C' || reply1 == 'c') {
		 	grade1 = 2;
		 	}
		 if (reply1 == 'D' || reply1 == 'd') {
		 	grade1 = 1;
		 	}
		 if (reply1 == 'F' || reply1 == 'f') {
		 	grade1 = 0;}
		 	}
		 
		System.out.print("What is the Second period grade? ");
		reply2 = myScanner.findInLine(".").charAt(0);
		
	   { if (reply2 == 'A' || reply2 == 'a') {grade2 = 4;} 
		 if (reply2 == 'B' || reply2 == 'b') {grade2 = 3;}
		 if (reply2 == 'C' || reply2 == 'c') {grade2 = 2;}
		 if (reply2 == 'D' || reply2 == 'd') {grade2 = 1;} 
		 if (reply2 == 'F' || reply2 == 'f') {grade2 = 0;} }
		
		System.out.print("What is the Third period grade? ");
		reply3 = myScanner.findInLine(".").charAt(0);
		
	   { if (reply3 == 'A' || reply3 == 'a') {grade3 = 4;} 
		 if (reply3 == 'B' || reply3 == 'b') {grade3 = 3;} 
		 if (reply3 == 'C' || reply3 == 'c') {grade3 = 2;} 
		 if (reply3 == 'D' || reply3 == 'd') {grade3 = 1;} 
		 if (reply3 == 'F' || reply3 == 'f') {grade3 = 0;} }
		
		System.out.print("What is the Fourth period grade? ");
		reply4 = myScanner.findInLine(".").charAt(0);
		
		 if (reply4 == 'A' || reply4 == 'a') {grade4 = 4;};
		 if (reply4 == 'B' || reply4 == 'b') {grade4 = 3;};
		 if (reply4 == 'C' || reply4 == 'c') {grade4 = 2;};
		 if (reply4 == 'D' || reply4 == 'd') {grade4 = 1;};
		 if (reply4 == 'F' || reply4 == 'f'); {grade4 = 0;}
		
		System.out.print("What is the Fifth period grade? ");
		reply5 = myScanner.findInLine(".").charAt(0);
		
	   { if (reply5 == 'A' || reply5 == 'a') {grade5 = 4;} 
		 if (reply5 == 'B' || reply5 == 'b') {grade5 = 3;} 
	     if (reply5 == 'C' || reply5 == 'c') {grade5 = 2;} 
		 if (reply5 == 'D' || reply5 == 'd') {grade5 = 1;} 
		 if (reply5 == 'F' || reply5 == 'f') {grade5 = 0;} }
		
		System.out.print("What is the Sixth period grade? ");
		reply6 = myScanner.findInLine(".").charAt(0);
	 	
	   { if (reply6 == 'A' || reply6 == 'a') {grade6 = 4;}
	 	 if (reply6 == 'B' || reply6 == 'b') {grade6 = 3;}
	 	 if (reply6 == 'C' || reply6 == 'c') {grade6 = 2;} 
	 	 if (reply6 == 'D' || reply6 == 'd') {grade6 = 1;} 
	     if (reply6 == 'F' || reply6 == 'f') {grade6 = 0;} } 
		
		
		GPA = ((grade1 + grade2 + grade3 + grade4 + grade5 + grade6)/6);
		
		System.out.print("Your child's GPA is ");
		System.out.print(GPA);
	}
}

Some of your if statements look like this: { if (someCondition) { /* ... */ } . Get rid of that first bracket so it's just if (someCondition) { /* ... */ }

If this is what you ment,

import java.util.Scanner;

class grades {
	
	public static void main(String args[]) {
		Scanner myScanner = new Scanner(System.in);
		int grade1;
		int grade2;
		int grade3;
		int grade4;
		int grade5;
		int grade6;
		char reply1;
		char reply2;
		char reply3;
		char reply4;
		char reply5;
		char reply6;
		double GPA;
				
		System.out.print("What is the First period grade? ");
		reply1 = myScanner.findInLine(".").charAt(0);
		
		 if (reply1 == 'A' || reply1 == 'a') {grade1 = 4;}
		 if (reply1 == 'B' || reply1 == 'b') {grade1 = 3;}
		 if (reply1 == 'C' || reply1 == 'c') {grade1 = 2;}
		 if (reply1 == 'D' || reply1 == 'd') {grade1 = 1;}
		 if (reply1 == 'F' || reply1 == 'f') {grade1 = 0;}
		 	
		 
		System.out.print("What is the Second period grade? ");
		reply2 = myScanner.findInLine(".").charAt(0);
		
	     if (reply2 == 'A' || reply2 == 'a') {grade2 = 4;} 
		 if (reply2 == 'B' || reply2 == 'b') {grade2 = 3;}
		 if (reply2 == 'C' || reply2 == 'c') {grade2 = 2;}
		 if (reply2 == 'D' || reply2 == 'd') {grade2 = 1;} 
		 if (reply2 == 'F' || reply2 == 'f') {grade2 = 0;} 
		
		System.out.print("What is the Third period grade? ");
		reply3 = myScanner.findInLine(".").charAt(0);
		
	     if (reply3 == 'A' || reply3 == 'a') {grade3 = 4;} 
		 if (reply3 == 'B' || reply3 == 'b') {grade3 = 3;} 
		 if (reply3 == 'C' || reply3 == 'c') {grade3 = 2;} 
		 if (reply3 == 'D' || reply3 == 'd') {grade3 = 1;} 
		 if (reply3 == 'F' || reply3 == 'f') {grade3 = 0;} 
		
		System.out.print("What is the Fourth period grade? ");
		reply4 = myScanner.findInLine(".").charAt(0);
		
		 if (reply4 == 'A' || reply4 == 'a') {grade4 = 4;};
		 if (reply4 == 'B' || reply4 == 'b') {grade4 = 3;};
		 if (reply4 == 'C' || reply4 == 'c') {grade4 = 2;};
		 if (reply4 == 'D' || reply4 == 'd') {grade4 = 1;};
		 if (reply4 == 'F' || reply4 == 'f'); {grade4 = 0;}
		
		System.out.print("What is the Fifth period grade? ");
		reply5 = myScanner.findInLine(".").charAt(0);
		
	     if (reply5 == 'A' || reply5 == 'a') {grade5 = 4;} 
		 if (reply5 == 'B' || reply5 == 'b') {grade5 = 3;} 
	     if (reply5 == 'C' || reply5 == 'c') {grade5 = 2;} 
		 if (reply5 == 'D' || reply5 == 'd') {grade5 = 1;} 
		 if (reply5 == 'F' || reply5 == 'f') {grade5 = 0;} 
		
		System.out.print("What is the Sixth period grade? ");
		reply6 = myScanner.findInLine(".").charAt(0);
	 	
	     if (reply6 == 'A' || reply6 == 'a') {grade6 = 4;}
	 	 if (reply6 == 'B' || reply6 == 'b') {grade6 = 3;}
	 	 if (reply6 == 'C' || reply6 == 'c') {grade6 = 2;} 
	 	 if (reply6 == 'D' || reply6 == 'd') {grade6 = 1;} 
	     if (reply6 == 'F' || reply6 == 'f') {grade6 = 0;} 
		
		
		GPA = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6) /6;
		
		System.out.print("Your child's GPA is ");
		System.out.print(GPA);
	
}
}

then I did that and i'm still getting the same "variable might not have been initialized"

when we use the variables to compare somethig in Java and they do not have some value already given to them..then this error is likely to occur..
Just initialize the int variable with 0
e.g., int grade1=0;

and the char variables with blank space..
e.g, char reply1=' ';

and then try compiling it..!!

Member Avatar for iamthwee

and the char variables with blank space..
e.g, char reply1=" ";

Isn't that supposed to be enclosed with single quotation marks? Otherwise the compiler will think it is a string?

char reply1=' ';

yes sure..i'll edit that right now..
thanks!!! it just slipped out of my mind.

I took psodhi's advice and I got it working, I also added extra semicolons on the fourth period grade part and I changed the
int grade1, int grade2, etc. into double grade1, double grade2, etc. I also added else for extra precaution.

here is what I got as the finished code, for anybody who cares besides me.

import java.util.Scanner;

class grades {
	
	public static void main(String args[]) {
		Scanner myScanner = new Scanner(System.in);
		double grade1=0;
		double grade2=0;
		double grade3=0;
		double grade4=0;
		double grade5=0;
		double grade6=0;
		char reply1;
		char reply2;
		char reply3;
		char reply4;
		char reply5;
		char reply6;
		double GPA;
				
		System.out.print("What is the First period grade? "); 
		reply1 = myScanner.findInLine(".").charAt(0);
		
		 if (reply1 == 'A' || reply1 == 'a') {grade1 = 4;}else{
		 if (reply1 == 'B' || reply1 == 'b') {grade1 = 3;}else{
		 if (reply1 == 'C' || reply1 == 'c') {grade1 = 2;}else{
		 if (reply1 == 'D' || reply1 == 'd') {grade1 = 1;}else{
		 if (reply1 == 'F' || reply1 == 'f') {grade1 = 0;}}}}}
		 	
		 
		System.out.print("What is the Second period grade? "); 
		reply2 = myScanner.findInLine(".").charAt(0);
		
	     if (reply2 == 'A' || reply2 == 'a') {grade2 = 4;}else{ 
		 if (reply2 == 'B' || reply2 == 'b') {grade2 = 3;}else{
		 if (reply2 == 'C' || reply2 == 'c') {grade2 = 2;}else{
		 if (reply2 == 'D' || reply2 == 'd') {grade2 = 1;}else{
		 if (reply2 == 'F' || reply2 == 'f') {grade2 = 0;}}}}}
		
		System.out.print("What is the Third period grade? ");
		reply3 = myScanner.findInLine(".").charAt(0);
		
	     if (reply3 == 'A' || reply3 == 'a') {grade3 = 4;}else{ 
		 if (reply3 == 'B' || reply3 == 'b') {grade3 = 3;}else{
		 if (reply3 == 'C' || reply3 == 'c') {grade3 = 2;}else{ 
		 if (reply3 == 'D' || reply3 == 'd') {grade3 = 1;}else{
		 if (reply3 == 'F' || reply3 == 'f') {grade3 = 0;}}}}}
		
		System.out.print("What is the Fourth period grade? ");
		reply4 = myScanner.findInLine(".").charAt(0);
		
		 if (reply4 == 'A' || reply4 == 'a') {grade4 = 4;}else{
		 if (reply4 == 'B' || reply4 == 'b') {grade4 = 3;}else{
		 if (reply4 == 'C' || reply4 == 'c') {grade4 = 2;}else{
		 if (reply4 == 'D' || reply4 == 'd') {grade4 = 1;}else{
		 if (reply4 == 'F' || reply4 == 'f') {grade4 = 0;}}}}}
		
		System.out.print("What is the Fifth period grade? ");
		reply5 = myScanner.findInLine(".").charAt(0);
		
	     if (reply5 == 'A' || reply5 == 'a') {grade5 = 4;}else{ 
		 if (reply5 == 'B' || reply5 == 'b') {grade5 = 3;}else{
	     if (reply5 == 'C' || reply5 == 'c') {grade5 = 2;}else{
		 if (reply5 == 'D' || reply5 == 'd') {grade5 = 1;}else{
		 if (reply5 == 'F' || reply5 == 'f') {grade5 = 0;}}}}}
		
		System.out.print("What is the Sixth period grade? ");
		reply6 = myScanner.findInLine(".").charAt(0);
	 	
	     if (reply6 == 'A' || reply6 == 'a') {grade6 = 4;}else{
	 	 if (reply6 == 'B' || reply6 == 'b') {grade6 = 3;}else{
	 	 if (reply6 == 'C' || reply6 == 'c') {grade6 = 2;}else{
	 	 if (reply6 == 'D' || reply6 == 'd') {grade6 = 1;}else{
	     if (reply6 == 'F' || reply6 == 'f') {grade6 = 0;}}}}}
		
		
		GPA = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6)/6 ;
		
		System.out.print("Your child's GPA is ");
		System.out.print(GPA);
	
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.