Java, Java, Fun, Fun

Argh, I have my coding all done except for two last parts, and an if statement isn’t working!!!! can anyone see what I did wrong???

import java.io.*;
import java.util.*;

public class Students
{
public static void main (String[] args) throws Exception
{
int z = 0;
String a = “notnull”;
BufferedReader stdin = new BufferedReader(new FileReader(args[0]));

a = stdin.readLine();
if (a != null) {z++;}

while (a != null)
{
a = stdin.readLine();
if (a != null) {z++;}
}

System.out.println (“# of Students: ” + z);
Student [] stuarr = new Student [z];

BufferedReader stdin2 = new BufferedReader(new FileReader(args[0]));

String type = ” “, lastname = ” “, initials = ” “, banner = ” “, supervisor = ” “;
int prerequisits = 0;
a = stdin2.readLine();
if (a != null)
{
StringTokenizer TokenOf = new StringTokenizer (a);
if (TokenOf.hasMoreTokens())
{
type = TokenOf.nextToken().trim();

if (TokenOf.hasMoreTokens())
{
lastname = TokenOf.nextToken();
}

if (TokenOf.hasMoreTokens())
{
initials = TokenOf.nextToken();
}

if (TokenOf.hasMoreTokens())
{
banner = TokenOf.nextToken();
}

if (TokenOf.hasMoreTokens())
{
if (type == “c”)
{prerequisits = Integer.parseInt(TokenOf.nextToken().trim());}
else if (type == “g”)
{supervisor = TokenOf.nextToken().trim();}
}

if (type == “n”)
{stuarr[0] = new Student(lastname, initials, banner);}
else if (type == “c”)
{stuarr[0] = new Conditional(lastname, initials, banner, prerequisits);}
else if (type == “g”)
{stuarr[0] = new Graduate(lastname, initials, banner, supervisor);}

}
}

while (a != null)
{
a = stdin2.readLine();
if (a != null) {z++;}
}

displayArray ( stuarr );
int x = insertionSortStudents ( stuarr );
System.out.println (“Comparisons: ” + x);
displayArray ( stuarr );

}

public static void displayArray( Student a [] ) throws Exception
{
if (a.length == 0) throw new EmptyArrayException (“Empty array to insertionSortStudents”);
System.out.println (“——————–“);
for (int i=0; i < a.length; i++) { Student temp = a[i]; temp.display(); System.out.println ("--------------------"); } } public static int insertionSortStudents ( Comparable [] x ) throws Exception { int n = 0; if (x.length == 0) throw new EmptyArrayException ("Empty array to insertionSortStudents"); for ( int i = 1 ; i < x.length; i++ ) { Student temp = (Student)x [i]; int j = i; for ( n++; ( j > 0 && temp.compareTo ( x[j-1] ) < 0 ) ; j-- ) { x[j] = x[j-1]; n ++; } x[j] = temp; } return n; } } class Student implements Comparable { String name; String ID; String initials; public Student(String nom, String init, String stuid) { name = nom; ID = stuid; initials = init; } public String toString() { String daString; daString = (name + ", " + initials + ", " + ID); return daString; } public void display() { System.out.println ("Last Name: " + name); System.out.println ("Initials: " + initials); System.out.println ("Student ID: " + ID); } public int compareTo(Object rhs) { Student r = (Student)rhs; if (name.compareTo(r.name) == 0) { return initials.compareTo(r.initials); } else return (name.compareTo(r.name)); } } class Conditional extends Student { int prereqs; public Conditional(String nom, String init, String stuid, int pre) { super(nom, init, stuid); prereqs = pre; } public String toString() { String daString; daString = (super.toString() + " " + prereqs); return daString; } public void display() { super.display(); System.out.println ("Prereq Courses: " + prereqs); } } class Graduate extends Student { String supervisor; public Graduate(String nom, String init, String stuid, String Super) { super(nom, init, stuid); supervisor = Super; } public String toString() { String daString; daString = (super.toString() + " " + supervisor); return daString; } public void display() { super.display(); System.out.println ("Supervisor: " + supervisor); } } class EmptyArrayException extends Exception { public EmptyArrayException(String emptydia) { super(emptydia); } }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.