I compiled this program 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

/*
*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
	{
		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");
			
			//total student courses
			n = 6; 
			int i=0; 
			//numbers of total of A, B, C, D & F
			int gpA=0;
			int gpB=0;
			int gpC=0;
			int gpD=0;
			int gpF=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; gpA++; break;
					case 'B': gp=3; gpB++; break;
					case 'C': gp=2; gpC++; break;
					case 'D': gp=1; gpD++; break;
					case 'F': gp=0; gpF++; 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);
			
			//print divider for output the summary of letGrade blank lines
		outFile.println();
		outFile.println();
		outFile.println();
		outFile.println("____________________________________");
		outFile.println("The Grades sort out for all the students at BCCC "+ "this Spring semester are: " );
		outFile.println(gpA+", "+gpB+", "+", "+gpC+", "+gpD+", "+gpF);
	 	
		}//End While loop then close the inputgpa file
		inFile.close();
		
				
	}//End Main method
	
}

Recommended Answers

All 12 Replies

"it give me some kind of problem"
what problem EXACTLY?

"it give me some kind of problem"
what problem EXACTLY?

Hi James,
This what I got in the console:

----jGRASP exec: java gpa
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:840)
	at java.util.Scanner.next(Scanner.java:1461)
	at java.util.Scanner.nextInt(Scanner.java:2091)
	at java.util.Scanner.nextInt(Scanner.java:2050)
	at gpa.main(gpa.java:54)

 ----jGRASP wedge: exit code for process is 1.
 ----jGRASP: operation complete.

That exception tells you that the scanner encountered something that was not valid for the way you wanted to use it, eg you called nextInt() but the next thing in the input stream could not be interpreted as an integer.
The line number in your error message doesn't match the version of the code you posted, and I don't know what you typed at the console, so there's no more I can say for now.
J

That exception tells you that the scanner encountered something that was not valid for the way you wanted to use it, eg you called nextInt() but the next thing in the input stream could not be interpreted as an integer.
The line number in your error message doesn't match the version of the code you posted, and I don't know what you typed at the console, so there's no more I can say for now.
J

I'm modifying my program right now and just quick block them with comment syntax (//) so that I can run it to show you the results, that would explain why my line code are not match. Sorry for that!
About the stream input: it's just the string of 7 numbers in a separate line (I'll post here 1st Name, 2nd idNumber, 3rd courseName & letGrade)which look like this:
DAVID JOHNSON
1234567
CISC115.59 A
I don't know other way to read int idNumber and is it because of that I can't run my program? May you help!?

That's the right way to read an integer. But let's see what the next test gives via matching error messages and source code line numbers.
Are you sure its not failing on the earlier
numStudents = inFile.nextInt();

That's the right way to read an integer. But let's see what the next test gives via matching error messages and source code line numbers.
Are you sure its not failing on the earlier
numStudents = inFile.nextInt();

OK, James
This is the original program and its resulted after run. I also include first student in the inputgpa.txt

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; //to read id from file
		int id; //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");
			
			//total student courses
			n = 6; 
			int i=0; 
			//numbers of total of A, B, C, D & F
			int gpA=0;
			int gpB=0;
			int gpC=0;
			int gpD=0;
			int gpF=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; gpA++; break;
					case 'B': gp=3; gpB++; break;
					case 'C': gp=2; gpC++; break;
					case 'D': gp=1; gpD++; break;
					case 'F': gp=0; gpF++; 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);
			
			//print divider for output the summary of letGrade blank lines
		outFile.println();
		outFile.println();
		outFile.println();
		outFile.println("____________________________________");
		outFile.println("The Grades sort out for all the students at BCCC "+ "this Spring semester are: " );
		outFile.println(gpA+", "+gpB+", "+", "+gpC+", "+gpD+", "+gpF);
	 	
		}//End While loop then close the inputgpa file
		inFile.close();
				
		
	}//End Main method
	
}

Result after run

----jGRASP exec: java gpa
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:840)
	at java.util.Scanner.next(Scanner.java:1461)
	at java.util.Scanner.nextInt(Scanner.java:2091)
	at java.util.Scanner.nextInt(Scanner.java:2050)
	at gpa.main(gpa.java:55)

 ----jGRASP wedge: exit code for process is 1.
 ----jGRASP: operation complete.

10
DAVID JOHNSON
1234567
CISC115.59 A
VAGD102.11 F
VAMM100.23 B
MKTG100.41 B
VAMM120.15 A
VAMM130.20 D

Make a printout of your input file, or just get it on screen next to the source file. Read through your source code and check it against the input file, line by line. You should see the problem pretty quickly.

Make a printout of your input file, or just get it on screen next to the source file. Read through your source code and check it against the input file, line by line. You should see the problem pretty quickly.

It took me... I don't know how many hours trying different things changing from Float to modifying things around. It is 12:30 am now, I'm still sitting here and couldn't figure out why? It maybe easy for you but for beginner...just stuck.

It took me... I don't know how many hours trying different things changing from Float to modifying things around. It is 12:30 am now, I'm still sitting here and couldn't figure out why? It maybe easy for you but for beginner...just stuck.

OK, I figured out after awhile.
instead of nextLine(); I use only next(); But because I have full name with space in between, so I have 2 next(); ( I would think nextLine make more sense cause it took the whole line)

I should be off for writing arrays collecting grades for print out but it too late now. See you during the day.

I would think nextLine make more sense

Me too, Can anyone explain this?

So I start my Arrays...
The array suppose to collect all the students letGrade and print out. My problem is where should I collecting data, inside the switch? and how ? At first I only use gpA++ inside the switch(line 74 to 79) but it only give me the individual result at the bottom of each student. like this:
DAVID JOHNSON
12345

Couse Grade Gradepoints
CISC115.59 A 4.0
VAGD102.11 F 0.0
VAMM100.23 B 3.0
MKTG100.41 B 3.0
VAMM120.15 A 4.0
VAMM130.20 D 1.0
------------------------------------------
GPA: 2.5
____________________________________
The Grades sort out for all the students at BCCC this Spring semester are:
2As, 2Bs, 0Cs, 1Ds, 1Fs

switch(g)
				{
					case 'A': gp=4; gpA++; break;
					case 'B': gp=3; gpB++; break;
					case 'C': gp=2; gpC++; break;
					case 'D': gp=1; gpD++; break;
					case 'F': gp=0; gpF++; break;
					default: System.out.println("Invalid Grade");
				}

But then I need to create an array so I do this (below) but it doesn't work

//Arrays of total of A, B, C, D & F
			int []gpA;
			int []gpB;
			int []gpC;
			int []gpD;
			int []gpF;
						
			//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; gpA; break;
					case 'B': gp=3; gpB; break;
					case 'C': gp=2; gpC; break;
					case 'D': gp=1; gpD; break;
					case 'F': gp=0; gpF; break;
					default: System.out.println("Invalid Grade");
				}//End switch loop
int []gpA;

You've declared the array, but you haven't intialized it. At this point, the compiler knows that gpA is meant to point to an array of int, but it doesn't have a location for it, so it points to null.

More importantly, I don't think I understand what your design is doing. What does each array of ints represent? gpA is a collection of integers - what does each of those ints mean? What range of values do you expect to find in gpA[0], gpA[1], etc?

case 'A': gp=4; gpA;

I'm not sure what you're trying to do here.

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.