Please help with this program!
I wrote & modified this code many time but not quite find a good solution for it. Below are some error after complied.
Program problem:
Input: student names, student IDs, their courses and letter grades
Output to a file in table layout format with: College name, student names, student IDs, grade point, their gpa, number of total A's, B's, C's ect.

Any idea on how I should do the format layout out side of the loop for total students? Thanks

import java.text.*; // format output
import java.util.*; // Scanner class
import java.io.*; //Needed for File classes


public class gpa
{
	public static void main(String[] args)throws Exception
	{
		DecimalFormat num=new DecimalFormat("#######.00"); // Create format for name num (courseName)
		DecimalFormat numgpa=new DecimalFormat("##.00"); // Create format for name numgpa (gpa)
						

		// variable names 

		double gp=0.0; // grade point 
		double totalgp=0.0; // total grade point
		double n=0.0; //number of courses
		double gpa=0.0; //greade point average
		String str; // to hold answer input to the prompt
		String name; // student name in file
		String firstName; // student first name
		String lastName; // student last name
		String inGrade; //input grade
		String courseName; //course name
		int courseSection; //section number of the course
		char repeat; //to hold yes 'Y' or no 'N' answer 
		char letGrade; //input letter grade
		int idNumber=0; //student ID number
		int i=0; //number of courses input
		int h=0; //number of student name in the file	
			
		
		// declare scan & associate with actual data file
		File scan = new File ("inputgpa.txt");
		
		// Initialize scanner to read from file
		Scanner input = new scanner(scan);
		

		// Open file and continue if there is more data in file
		while (input.hasNext())
		{
			for(h=0; h<name.length; h++)
			{
				//Read name from file
				name = input.nextLine();
			
				// Initialize Scanner to read from DOS window.
				Scanner input = new Scanner(System.in);
				
				//Dispay the student name to DOS window
				System.out.print(" Student name is: "+ name);
				
				//Input the student ID number
				System.out.print("Enter"+name+"'s"+" student ID Number:	");
				idNumber = input.nextInt();
				
				//Output name & id to DOS window
				System.out.println("\tBCCC "+"Grade Report For ");//1st row 
				System.out.println("\t\t\t"+name); //2nd row 
				System.out.println("\t\t\t"+idNumber); //3rd row
				
				//Input number of courses
				System.out.print("Enter the number of courses: ");
				n = input.nextInt();
				
				//Start count loop to read all courses & their letter grades
				for(i=0; i<=n; i++)
				{
					//write prompt for user input course name
					System.out.print("Enter the number of courses:	");
					courseName = input.next();
					
					//input section number of the course
					System.out.print("Enter the section number of this course:	");
					courseSection = input.nextInt();
					
					//input section number of the course
					System.out.print("Enter the letter grade of this course:	");
					letGrade = input.next().charAt(0);
					
					//compute gp 
					switch(letGrade)
					{
						case 'A': gp=4; break;
						case 'B': gp=3; break;
						case 'C': gp=2; break;
						case 'D': gp=1; break;
						case 'F': gp=0; break;
						default: System.out.println("Invalid course grade");					
					} //end Switch loop
				
					// Compute to find the total grade points.
					totalgp+= gp;	
					
					//Output layout on the DOS window
					System.out.println(courseName+"."+courseSection+" "+letGrade+" "+gp);
									
				}//End of 2nd For loop
				
				//compute gpa
				gpa = totalgp / n;
				
				//output layout & gpa to DOS window
				System.out.println("--------------------------------------");
				System.out.println("\tGPA: "+ gpa);	
				System.out.println(); //Prints a blank line
					
			}//End of 1st For loop
		}//End of While loop
						
	}//closemain method
}//close class gpa

----jGRASP exec: javac -g gpa.java
gpa.java:53: cannot find symbol
symbol : class scanner
location: class gpa
Scanner input = new scanner(scan);
^
gpa.java:59: cannot find symbol
symbol : variable length
location: class java.lang.String
for(h=0; h<name.length; h++)
^
gpa.java:65: input is already defined in main(java.lang.String[])
Scanner input = new Scanner(System.in);
^
3 errors

Recommended Answers

All 2 Replies

Hints on correcting those 3 errors: Check spelling, length is a method of String instead of a property, and you can't define the same variable more than once within a block.

Also, I would condense your large collection of variables into a separate class, and make some of your variable names more descriptive. "int h=0; //number of student name in the file" is completely unintuitive.

In terms of the formatting of the output, you can use the String.format() method to get your tabular format - it's just a matter of creating the right format pattern and passing the values to the format method, then perhaps use a PrintWriter to output your strings. Read through the classes in the java.io package to find the best writer that will suit your needs.

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.