1,105,169 Community Members

Read & Write to File, part 2

Member Avatar
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Seem tangle myself up with coding here and couldn't solve much of problem after many long try. I need your fresh set of eyes. Below is my errors. Thanx

/*
*Description: Writing own method, Switch Selection Control, String,Accumulating,
*Counting method, array base on last reviews from previous labs.
*prepare a grade report for more than 1 student using input File and
*output to a File with print outs letter grades # of A's, B's etc.
*Input: College Name, Student Name, Student ID Number and
*Courses-Section Name (ex. CISC115.59), Letter Grades
*Output: College Name, Student Name, Student ID Number;
*Courses, Letter Grades, Grade Points, GPA and total students grades.
*/

import java.io.*; //Needed for File classes
import java.util.*; //Needed for scanner classes




public class gpa
{
	public static void main(String[] args)throws Exception
	{									
					
		// declare inFile & associate with actual data file
		Scanner inFile = new Scanner(new FileReader("inputgpa.txt"));
		
		//Read numStudents from first line of inputgpa file
		int numStudents = inFile.nextInt(); //total number of students
		
		// declare outFile & associate with actual data file
		PrintWriter outFile = new PrintWriter("outputgpa.txt");
		
		//Print College name to file
		outFile.println("BCCC"+"Grade Report for "+ numStudents+"students");
				

		// Continue if there is more data in inputgpa file
		while (inFile.hasNext())
		{
			//Read Name & idNumber from inputgpa file
			String Name = inFile.nextLine(); // student name in file
			int idNumber = inFile.nextInt(); //student ID number
			
			// Output to file
			outFile.println("\t\t\t"+Name);
			outFile.println("\t\t\t"+idNumber);
			
			//print 1 blank line
			outFile.println();
			
			//Write to file for headings 
			outFile.println("Couse\t\t"+"Grade\t\t"+"Gradepoints");
			
			//Call studentCourses method for student gp & gpa calculation
			studentCourses();
			
		}//End While loop then close the inputgpa file
		inFile.closed();
		
	}//End Main method
	
	
	/** ___________________________________________________________
	 This loop for colecting the 6 courseName and their letGrade of
	 each student from the inputgpa file, then calculate the gp & gpa
	 */
	 public static void studentCourses()
	 {
	 	//continue read from inputgpa file
		while(inFile.hasNext())
		{
			//Declare variable n & its value
			int n = 6; //total of student courses
			int i=0; 
			//start loop
			for(i=0; i<n; i++)
			{
				//read courseName
				String courseName = inFile.nextLine();
				String letGrade = inFile.nextLine();
			
				//Pass the array to the gradeSortout method
				gradeSortout();
				
				double gp=0.0; // grade point
			
				//compute gp from input letGrade
				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 Grade"); break;
				}//End switch loop
			
			//compute to find total gradepoints
			double totalgp = 0.0;
			totalgp+= gp;
			
			//Read values then output to outputgp file
			outFile.println(courseName+"\t\t"+letGrade+"\t\t"+gp);
			}//End For loop
			
			//Compute gpa
			double gpa = 0.0;
			gpa = totalgp / n;
			
			//Read gpa and output to outputgpa file
			//print line
			outFile.println("-------------------------------");
			
			//display gpa as footer
			outFile.println("GPA: "+gpa);
			
	 	}//End While loop
	}//End studentCourses method
	
	// ___________________________________________________________________
	//Begin gradeSortout method  

	
	public static void gradeSortout()
	{
		// declare outGrade & associate with actual data file
		PrintWriter outGrade = new PrintWriter("Grade.txt");
		
		//Print to Grade file
		outGrade.println("BCCC"+"Grade Results Report of "+ numStudents+"students "+"- Spring 2011-");
		
		//calculate the total grades of all students for array uses
		int sum = numStudent*n;
		int[]gradeArray = new int[numStudent*n];
		int j=0;
		
		//start loop for gradeSortout
		for(j=0; j<sum; j++)
		{
			gradeArray[j] = 0;
			
			switch(letGrade)
			{
				case'A':outGrade.format(format,(j+1)+"A"); break;
				case'B':outGrade.format(format,(j+1)+"A"); break;
				case'C':outGrade.format(format,(j+1)+"A"); break;
				case'D':outGrade.format(format,(j+1)+"A"); break;
				case'F':outGrade.format(format,(j+1)+"A"); break;
				default:outGrade.println("invalid grade"); break;
			}
			
			//print to Grade file in table layout
			outGrade.println("________________________________________");
			outGrade.println();//one blank line
			
			String format = "%1$-10s%2$-10s%3$-10s%4$-10s%5$-10s%6$-10s\n";
			
			//print headings
			outGrade.format(format,"Number","A Grade", "B Grade", "C Grade", "D Grade", "F Grade");
			
			 
		}//End for loop		
		
	}//End gradeSortout method
	
}//End

gpa.java:73: cannot find symbol
symbol : variable inFile
location: class gpa
while(inFile.hasNext())
^
gpa.java:82: cannot find symbol
symbol : variable input
location: class gpa
String courseName = input.nextLine();
^
gpa.java:83: cannot find symbol
symbol : variable input
location: class gpa
String letGrade = input.nextLine();
^
gpa.java:86: gradeSortout(int[]) in gpa cannot be applied to ()
gradeSortout();
^
gpa.java:91: incompatible types
found : java.lang.String
required: int
switch(letGrade)
^
gpa.java:106: cannot find symbol
symbol : variable outFile
location: class gpa
outFile.println(courseName+"\t\t"+letGrade+"\t\t"+gp);
^
gpa.java:111: cannot find symbol
symbol : variable totalgp
location: class gpa
gpa = totalgp / n;
^
gpa.java:115: cannot find symbol
symbol : variable outFile
location: class gpa
outFile.println("-------------------------------");
^
gpa.java:118: cannot find symbol
symbol : variable outFile
location: class gpa
outFile.println("GPA: "+gpa);
^
gpa.java:134: cannot find symbol
symbol : variable numStudents
location: class gpa
outGrade.println("BCCC"+"Grade Results Report of "+ numStudents+"students "+"- Spring 2011-");
^
gpa.java:137: cannot find symbol
symbol : variable numStudent
location: class gpa
int sum = numStudent*n;
^
gpa.java:137: cannot find symbol
symbol : variable n
location: class gpa
int sum = numStudent*n;
^
gpa.java:138: cannot find symbol
symbol : variable numStudent
location: class gpa
int[]gradeArray = new int[numStudent*n];
^
gpa.java:138: cannot find symbol
symbol : variable n
location: class gpa
int[]gradeArray = new int[numStudent*n];
^
gpa.java:146: cannot find symbol
symbol : variable letGrade
location: class gpa
switch(letGrade)
^
gpa.java:148: cannot find symbol
symbol : variable format
location: class gpa
case'A':outGrade.format(format,(j+1)+"A"); break;
^
gpa.java:149: cannot find symbol
symbol : variable format
location: class gpa
case'B':outGrade.format(format,(j+1)+"A"); break;
^
gpa.java:150: cannot find symbol
symbol : variable format
location: class gpa
case'C':outGrade.format(format,(j+1)+"A"); break;
^
gpa.java:151: cannot find symbol
symbol : variable format
location: class gpa
case'D':outGrade.format(format,(j+1)+"A"); break;
^
gpa.java:152: cannot find symbol
symbol : variable format
location: class gpa
case'F':outGrade.format(format,(j+1)+"A"); break;

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

1st error: you declare inFile in main, then you try to use it in a different method. variables declared in a method only exist within that method. If you want to share variables between methods you must declare them in the class, but outside any one method.
Fix this, and any other errors of the same kind, then see what's left.
ps: Please ensure that the code you post corresponds to the error messages, otherwize it's hard to use the line numbers in the error messages.

Member Avatar
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I declared inFile with most of variables now in class cause I want to share them for 3 methods. Appear that I have more errors. Especially still at
Scanner inFile = new Scanner(new FileReader("inputgpa.txt"));

import java.io.*; //Needed for File classes




public class gpa
{	

	int idNumber; //student ID number
	String Name; //student name
	int numStudents; //number of total students
	int n=6; //total of courses in 1 student
	String courseName; //course name
	char letGrade; //course letter grade
	double gp=0.0; //gradepoints
	double totalgp = 0.0;
	double gpa = 0.0;
	
 	String inFile; //hold file inputgpa
	String outFile; //hold file outputgpa
														
								
	public static void main(String[] args)throws Exception
	{
		
		// declare inFile & associate with actual data file
		Scanner inFile = new Scanner(new FileReader("inputgpa.txt"));
		
		//Read numStudents from first line of inputgpa file
		numStudents = inFile.nextInt(); //total number of students
				
		// declare outFile & associate with actual data file
		PrintWriter outFile = new PrintWriter("outputgpa.txt");
			
		//Print College name to file
		outFile.println("BCCC"+"Grade Report for "+ numStudents+"students");
				

		// Continue if there is more data in inputgpa file
		while (inFile.hasNext())
		{
			//Read Name & idNumber from inputgpa file
			Name = inFile.nextLine(); // student name in file
			idNumber = inFile.nextInt(); //student ID number
			
			// Output to file
			outFile.println("\t\t\t"+Name);
			outFile.println("\t\t\t"+idNumber);
			
			//print 1 blank line
			outFile.println();
			
			//Write to file for headings 
			outFile.println("Couse\t\t"+"Grade\t\t"+"Gradepoints");
			
			//Call studentCourses method for student gp & gpa calculation
			studentCourses();
			
		}//End While loop then close the inputgpa file
		inFile.closed();
		
	}//End Main method
	
	
	/** ___________________________________________________________
	 This loop for colecting the 6 courseName and their letGrade of
	 each student from the inputgpa file, then calculate the gp & gpa
	 */
	 public static void studentCourses()
	 {
	 	//continue read from inputgpa file
		while(inFile.hasNext())
		{
			//Declare variable n & its value
			n = 6; //total of student courses
			int i=0; 
			//start loop
			for(i=0; i<n; i++)
			{
				//read courseName
				courseName = inFile.nextLine();
				letGrade = inFile.nextLine();
			
				//Pass the array to the gradeSortout method
				gradeSortout();
				
			
				//compute gp from input letGrade
				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 Grade"); break;
				}//End switch loop
			
			//compute to find total gradepoints
			totalgp+= gp;
			
			//Read values then output to outputgp file
			outFile.println(courseName+"\t\t"+letGrade+"\t\t"+gp);
			}//End For loop
			
			//Compute gpa
			gpa = totalgp / n;
			
			//Read gpa and output to outputgpa file
			//print line
			outFile.println("-------------------------------");
			
			//display gpa as footer
			outFile.println("GPA: "+gpa);
			
	 	}//End While loop
	}//End studentCourses method
	
	// ___________________________________________________________________
	//Begin gradeSortout method  

	
	public static void gradeSortout()
	{
		// declare outGrade & associate with actual data file
		PrintWriter outGrade = new PrintWriter("Grade.txt");
		
		//Print to Grade file
		outGrade.println("BCCC"+"Grade Results Report of "+ numStudents+"students "+"- Spring 2011-");
		
		//calculate the total grades of all students for array uses
		int sum = numStudent*n;
		int[]gradeArray = new int[numStudent*n];
		int j=0;
		
		//start loop for gradeSortout
		for(j=0; j<sum; j++)
		{
			gradeArray[j] = 0;
			
			switch(letGrade)
			{
				case'A':outGrade.format(format,(j+1)+"A"); break;
				case'B':outGrade.format(format,(j+1)+"A"); break;
				case'C':outGrade.format(format,(j+1)+"A"); break;
				case'D':outGrade.format(format,(j+1)+"A"); break;
				case'F':outGrade.format(format,(j+1)+"A"); break;
				default:outGrade.println("invalid grade"); break;
			}
			
			//print to Grade file in table layout
			outGrade.println("________________________________________");
			outGrade.println();//one blank line
			
			String format = "%1$-10s%2$-10s%3$-10s%4$-10s%5$-10s%6$-10s\n";
			
			//print headings
			outGrade.format(format,"Number","A Grade", "B Grade", "C Grade", "D Grade", "F Grade");
			
			 
		}//End for loop		
		
	}//End gradeSortout method
	
}//End

gpa.java:41: cannot find symbol
symbol : class Scanner
location: class gpa
Scanner inFile = new Scanner(new FileReader("inputgpa.txt"));
^
gpa.java:41: cannot find symbol
symbol : class Scanner
location: class gpa
Scanner inFile = new Scanner(new FileReader("inputgpa.txt"));
^
gpa.java:44: non-static variable numStudents cannot be referenced from a static context
numStudents = inFile.nextInt(); //total number of students
^
gpa.java:50: non-static variable numStudents cannot be referenced from a static context
outFile.println("BCCC"+"Grade Report for "+ numStudents+"students");
^
gpa.java:57: non-static variable Name cannot be referenced from a static context
Name = inFile.nextLine(); // student name in file
^
gpa.java:58: non-static variable idNumber cannot be referenced from a static context
idNumber = inFile.nextInt(); //student ID number
^
gpa.java:61: non-static variable Name cannot be referenced from a static context
outFile.println("\t\t\t"+Name);
^
gpa.java:62: non-static variable idNumber cannot be referenced from a static context
outFile.println("\t\t\t"+idNumber);
^
gpa.java:86: non-static variable inFile cannot be referenced from a static context
while(inFile.hasNext())
^
gpa.java:86: cannot find symbol
symbol : method hasNext()
location: class java.lang.String
while(inFile.hasNext())
^
gpa.java:89: non-static variable n cannot be referenced from a static context
n = 6; //total of student courses
^

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

You have lost your import for java.util.*, which is where Scanner is defined.
You still declare inFile in main, despite all that was said.
You now declare a String called inFile in your class. That's really confusing.
Finally
Your methods are static, which means they execute inthe context of the class itself, without any particular instance of the class.
You declare variables without the static keyword, so they belong to individual instances, so they cannot be used in static methods where there is no instance. That's why you get "non-static variable n cannot be referenced from a static context". Static methods need static variables.

Member Avatar
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

You have lost your import for java.util.*, which is where Scanner is defined.
You still declare inFile in main, despite all that was said.
You now declare a String called inFile in your class. That's really confusing.
Finally
Your methods are static, which means they execute inthe context of the class itself, without any particular instance of the class.
You declare variables without the static keyword, so they belong to individual instances, so they cannot be used in static methods where there is no instance. That's why you get "non-static variable n cannot be referenced from a static context". Static methods need static variables.

What did you mean by "You declare variables without the static keyword,..."? May I have an example?

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 
int numStudents; //number of total students

public static void main(String[] args)throws Exception {
  ...
  numStudents = inFile.nextInt();

>>> non-static variable numStudents cannot be referenced from a static context

numStudents is not static, you use it in a static "context" (method) >>> error

Member Avatar
jon.kiparsky
Posting Virtuoso
1,837 posts since Jun 2010
Reputation Points: 326 [?]
Q&As Helped to Solve: 192 [?]
Skill Endorsements: 6 [?]
 
1
 
int idNumber;

A field is either an instance field or a static field. An instance field belongs to a particular object, each time you make a new object of that class it has its own idNumber. Fields are instance fields by default.

If you prefix a field with the keyword "static":

static int idNumber;

you make it a static field. This means that there's only one of them, and all objects of that class have the same value for that field at any one time.

Methods are also instance or static methods. Instance methods are attached to one particular instance, which means they can refer to that instance's data. Static methods belong to the class as a whole, and therefore can refer only to static data and call static methods.

So when you declare your methods static so you can run them from your main method, you prohibit them from referring to the instance fields of the class.

This causes you a conundrum - how do you think you can run instance methods from the main method, if it's got to be static? Think about it a little and see if you can come up with a way to do that.

Member Avatar
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok!
I can't understand still, but...I just simplified it in hope that if you could walk me through from here would be easier for me.
I eliminated other methods down to just main, although I'd like to use it later for output of all students Grades.
Still have lots of errors

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

public class gpa
{
	public static void main(String[] args)throws Exception
	{						

		// variable names 

		double gp=0.0; // grade point 
		double totalgp=0.0; // total grade point
		int n=0; //number of courses
		double gpa=0.0; //greade point average
		String Name; // student name in file
		String courseName; //course name
		int numStudents; //total number of students
		char letGrade; //input letter grade
		int idNumber=0; //student ID number
		int i=0; 
					
		
		// declare scan & associate with actual data file
		File inFile = new File ("inputgpa.txt");
		
		// Initialize scanner to read from file
		Scanner input = new Scanner(inFile);
		
		//Read numStudents from first line in input gpa file 
		numStudents= inFile.nextInt();
		
		//declare outFile & associate with outputgpa file 
		PrintWriter outFile= new PrintWriter("outputgpa.txt");
		
		//Print College name to file
		outFile.println("BCCC"+"Grade Report for "+numStudents+" students");
		

		// Open file and continue if there is more data in file
		while (inFile.hasNext())
		{
			//Read Name & idNumber from File
			Name= inFile.nextLine();
			idNumber= inFile.nextInt();
			
			//Output to file
			outFile.println("\t\t\t"+Name);
			outFile.println("\t\t\t"+idNumber);
			
			//Print one blank line
			output.println();
			
			//Write to File for Headings
			output.println("Course\t\t"+"Grade\t\t"+"Gradepoints");
			
			
			//Start count loop to read all courses & their letter grades
			//gp & gpa calculation area
			n=6;	
			for(i=0; i<=n; i++)
			{
				//Read courseName
				courseName = inFile.nextString();
				letGrade = inFile.nextChar;
										
				//pass the array to gradeSortout method
				gradeSortout();
				
				
				//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"); break;					
				} //end Switch loop
				
				// Compute to find the total grade points.
				totalgp+= gp;	
					
				//Read values then output to outputgpa file
				outFile.println(courseName+"\t\t"+letGrade+"\t\t"+gp);
				
			}//End of For loop
				
			//compute gpa
			gpa = totalgp / n;
				
			//Read gpa the output to outputgpa file, print divider line
							
			outFile.println("--------------------------------------");
				
			//Display gpa as footer	
			outFile.println("\tGPA: "+ gpa);	
			outFile.println(); //Prints a blank line
					
		}//End of While loop
						
	}//closemain method
	
	
}//close class gpa
Member Avatar
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok!
I can't understand still, but...I just simplified it in hope that if you could walk me through from here would be easier for me.
I eliminated other methods down to just main, although I'd like to use it later for output of all students Grades.
Still have lots of errors

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

public class gpa
{
	public static void main(String[] args)throws Exception
	{						

		// variable names 

		double gp=0.0; // grade point 
		double totalgp=0.0; // total grade point
		int n=0; //number of courses
		double gpa=0.0; //greade point average
		String Name; // student name in file
		String courseName; //course name
		int numStudents; //total number of students
		char letGrade; //input letter grade
		int idNumber=0; //student ID number
		int i=0; 
					
		
		// declare scan & associate with actual data file
		File inFile = new File ("inputgpa.txt");
		
		// Initialize scanner to read from file
		Scanner input = new Scanner(inFile);
		
		//Read numStudents from first line in input gpa file 
		numStudents= inFile.nextInt();
		
		//declare outFile & associate with outputgpa file 
		PrintWriter outFile= new PrintWriter("outputgpa.txt");
		
		//Print College name to file
		outFile.println("BCCC"+"Grade Report for "+numStudents+" students");
		

		// Open file and continue if there is more data in file
		while (inFile.hasNext())
		{
			//Read Name & idNumber from File
			Name= inFile.nextLine();
			idNumber= inFile.nextInt();
			
			//Output to file
			outFile.println("\t\t\t"+Name);
			outFile.println("\t\t\t"+idNumber);
			
			//Print one blank line
			output.println();
			
			//Write to File for Headings
			output.println("Course\t\t"+"Grade\t\t"+"Gradepoints");
			
			
			//Start count loop to read all courses & their letter grades
			//gp & gpa calculation area
			n=6;	
			for(i=0; i<=n; i++)
			{
				//Read courseName
				courseName = inFile.nextString();
				letGrade = inFile.nextChar;
										
				//pass the array to gradeSortout method
				gradeSortout();
				
				
				//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"); break;					
				} //end Switch loop
				
				// Compute to find the total grade points.
				totalgp+= gp;	
					
				//Read values then output to outputgpa file
				outFile.println(courseName+"\t\t"+letGrade+"\t\t"+gp);
				
			}//End of For loop
				
			//compute gpa
			gpa = totalgp / n;
				
			//Read gpa the output to outputgpa file, print divider line
							
			outFile.println("--------------------------------------");
				
			//Display gpa as footer	
			outFile.println("\tGPA: "+ gpa);	
			outFile.println(); //Prints a blank line
					
		}//End of While loop
						
	}//closemain method
	
	
}//close class gpa

Below are listing of errors. appreciate any help .Thanks

gpa.java:46: cannot find symbol
symbol  : method nextInt()
location: class java.io.File
		numStudents= inFile.nextInt();
		                   ^
gpa.java:56: cannot find symbol
symbol  : method hasNext()
location: class java.io.File
		while (inFile.hasNext())
		             ^
gpa.java:59: cannot find symbol
symbol  : method nextLine()
location: class java.io.File
			Name= inFile.nextLine();
			            ^
gpa.java:60: cannot find symbol
symbol  : method nextInt()
location: class java.io.File
			idNumber= inFile.nextInt();
			                ^
gpa.java:67: cannot find symbol
symbol  : variable output
location: class gpa
			output.println();
			^
gpa.java:70: cannot find symbol
symbol  : variable output
location: class gpa
			output.println("Course\t\t"+"Grade\t\t"+"Gradepoints");
			^
gpa.java:79: cannot find symbol
symbol  : method nextString()
location: class java.io.File
				courseName = inFile.nextString();
				                   ^
gpa.java:80: cannot find symbol
symbol  : variable nextChar
location: class java.io.File
				letGrade = inFile.nextChar;
				                 ^
gpa.java:83: cannot find symbol
symbol  : method gradeSortout()
location: class gpa
				gradeSortout();
				^
Member Avatar
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Alright, I cut off all the other methods now. compiled with no error, but when I run it give me some kind of problem where I have: idNumber = inFile.nextInt(); //student ID number. Appreciate any help. Thanks

import java.io.*; //Needed for File classes
import java.util.*; //Needed for scanner classes


public class gpa
{	
							
	public static void main(String[] args)throws Exception
	{
		int idNumber; //student ID number
		String Name; //student name
		int numStudents; //number of total students
		int n=0; //total of courses of each student
		String courseName; //course name
		String letGrade; //course letter grade
		double gp=0.0; //gradepoints
		double totalgp = 0.0;//total gradepoints
		double gpa = 0.0;//GPA
	

		// declare inFile & associate with actual data file
		Scanner inFile = new Scanner(new FileReader("inputgpa.txt"));
		
		//Read numStudents from first line of inputgpa file
		numStudents = inFile.nextInt(); //total number of students
		
		// declare outFile & associate with actual data file
		PrintWriter outFile = new PrintWriter("outputgpa.txt");		
							
		//Print College name to file
		outFile.println(" "+"Grade Report for "+ numStudents+" students");
				

		// Continue if there is more data in inputgpa file
		while (inFile.hasNext())
		{
			//Read Name & idNumber from inputgpa file
			Name = inFile.nextLine(); // student name in file
			idNumber = inFile.nextInt(); //student ID number
			
			// Output to file
			outFile.println("\t\t\t"+Name);
			outFile.println("\t\t\t"+idNumber);
			
			//print 1 blank line
			outFile.println();
			
			//Write to file for headings 
			outFile.println("Couse\t\t"+"Grade\t\t"+"Gradepoints");
			
			//Declare variable n & its value
			n = 6; //total of student courses
			int i=0; 
			
			//start loop
			for(i=0; i<n; i++)
			{
				//read courseName
				courseName = inFile.next();
				letGrade = inFile.next();
				//change letGrade variable to character
				char g = letGrade.charAt(0);
							
				//compute gp from input letGrade
				switch(g)
				{
					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 Grade");
				}//End switch loop
			
			//compute to find total gradepoints
			totalgp+= gp;
			
			//Read values then output to outputgp file
			outFile.println(courseName+"\t\t"+letGrade+"\t\t"+gp);
			}//End For loop
			
			//Compute gpa
			gpa = totalgp / n;
			
			//Read gpa and output to outputgpa file
			//print line
			outFile.println("-------------------------------");
			
			//display gpa as footer
			outFile.println("GPA: "+gpa);
			
	 	
		}//End While loop then close the inputgpa file
		inFile.close();
		
	}//End Main method
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article