// Written by Mike Zrimsek.
//
// Solves CS 141, Fall 2011, Homework 7.
//
import java.io.*;
import java.util.Scanner;
/////////////////////////////////////////////////////////////////////////
class Hw07
{
//-----------------------------------------------------------------------
public static void main (String [] args) throws Exception
{
Scanner kb = new Scanner(System.in);
System.out.print("\nEnter name of a file of Students: ");
String filename = kb.nextLine();
Section sec = new Section(filename);
System.out.println("\nIn forward order:");
sec.print(System.out);
System.out.println("\nThat Section contains " +
sec.howMany() +
" Student(s).");
System.out.println("\nThat Section contains " +
sec.nPassing() +
" passing Student(s).");
System.out.println("\nThe highest grade is " +
sec.highestGrade() + ".");
}
//-----------------------------------------------------------------------
} // end class Hw07
/////////////////////////////////////////////////////////////////////////
class Section
{
private Student [] a;
private int used;
private static final int INIT_SIZE = 20;
//-----------------------------------------------------------------------
public Section ( String filename ) throws Exception
{
Scanner sc = new Scanner(new File(filename));
a = new Student[INIT_SIZE];
used = 0;
while ( sc.hasNext() )
{
Student s = Student.read(null,sc);
if ( used == a.length )
{
Student[] newA = new Student[2*a.length+1];
for ( int i = 0 ; i < used ; i++ ) newA[i] = a[i];
newA[used] = s;
used++;
a = newA;
}
else
{
a[used] = s;
used++;
}
}
}
//-----------------------------------------------------------------------
public void print ( PrintStream ps )
{
for ( int i = 0 ; i < used ; i++ )
{
ps.println(a[i]);
}
}
//-----------------------------------------------------------------------
public int howMany() { return used; }
//-----------------------------------------------------------------------
public double average ()
{
int sum = 0;
for ( int i = 0 ; i < used ; i++ )
{
Student s = a[i];
sum += s.getGrade();
}
return 1.0 * sum / used;
}
//-----------------------------------------------------------------------
public int nPassing()
{
int sum = 0;
int grade = 0;
for(int i = 0; i < a.length; i++)
{
grade = a[i].getGrade();
if(grade >= 60) sum++;
}
return sum;
}
//-----------------------------------------------------------------------
public int highestGrade()
{
int max = 0;
int grade = 0;
if(used == 0) throw new Error
("Attempt to find the maximum of an empty Section");
for(int i = 0; i < a.length; i++)
{
grade = a[i].getGrade();
if(grade > max) grade = max;
}
return max;
}
//-----------------------------------------------------------------------
} // end class Section
/////////////////////////////////////////////////////////////////////////
class Student
{
private String name;
private int grade;
//-----------------------------------------------------------------------
public Student ( String name, int grade )
{
this.name = name;
this.grade = grade;
}
//-----------------------------------------------------------------------
public String toString ()
{
return name + " (" + grade + ")";
}
//-----------------------------------------------------------------------
public String getName() { return name; }
//-----------------------------------------------------------------------
public int getGrade() { return grade; }
//-----------------------------------------------------------------------
public void setGrade( int newGrade ) { grade = newGrade; }
//-----------------------------------------------------------------------
public static Student read ( PrintStream ps, Scanner sc )
{
if ( ps != null ) ps.println("Reading a Student record ...");
if ( ps != null ) ps.print("Enter the name: ");
String name = sc.nextLine();
if ( ps != null ) ps.print("Enter the grade: ");
int grade = sc.nextInt(); //sc.nextLine();
return new Student(name,grade);
}
//-----------------------------------------------------------------------
} // end class Student
/////////////////////////////////////////////////////////////////////////
Error message when I run Student1.txt:
java.lang.NullPointerException at Section.nPassing(Hw07.java:100) at Hw07.main(Hw07.java:28)
Oddly, the above input file actually prints out the correct output, as I would expect, it just throws this error still.
Error message when I run Student3.txt and Student10.txt:
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 Student.read(Hw07.java:150) at Section.<init>(Hw07.java:53) at Hw07.main(Hw07.java:19)
Also, I realize the formatting is completely ridiculous, but that's the way my professor wants it and apparently he knows best. I realize it's late. I've been trying to debug my code for hours now and Dreamincode is really my last resort. Maybe someone can point me in the right direction here. Any help will be greatly appreciated.
For the input files, there are 3 different ones.
Student1.txt
Kevin
75
Student3.txt
Kevin
53
Susan
82
Nguyen
75
Student10.txt
Sally
100
Rico
82
Sue
95
Bill
43
Rob
67
Meng
94
Pam
82
Harvey
72
Rich
45
Debra
88

New Topic/Question
Reply




MultiQuote







|