Reputation:
It is called Marks Processing and basically my task is to write a program that reads in the information from three files and output a listing of the results for each student, ordered in descending order of overall mark.
I have saved the 3 Files and the first file contains student name and id, the next two files contains the coursework and exam marks. The output for each student should comprise two lines:
Line 1: Student id followed by student name
Line 2: Coursework, exam and aggregate marks for each module followed by overall mark
Marks should show one digit after the decimal point.
In the listing, the results for each student should be separated by a row of “-“. I had some code to make this work but the file cannot be found eventhough the files are all saved on my desktop.
The code I have is here:
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
class Student implements Comparable {
String name;
int id;
double ab101mark;
double ab102mark;
double aggregate;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
public int compareTo(Object obj) {
return (int)(((Student)obj).aggregate - this.aggregate);
}
public void setAggregate() {
aggregate = (ab101mark + ab102mark)/2;
}
public void printTranscript(PrintWriter destination) {
destination.println(id + "\t " + name);
destination.printf("AB101\t %4.1f\t AB102\t %4.1f\t Aggregate %4.1f\n", ab101mark, ab102mark, aggregate);
destination.println("----------------------------------------------------");
}
}
public class MarksProcessor {
private static Student findStudent(int idnum, ArrayList<Student> clist) {
for (Student s : clist) {
if (s.id == idnum) return s;
}
return null;
}
public static void main(String[] args) throws Exception {
ArrayList<Student> classList = new ArrayList<Student>();
File inFile = new File("C:/Users/mobin/Desktop/ABStudents.txt");
Scanner input = new Scanner(inFile);
while (input.hasNext()) {
int id = input.nextInt();
String name = input.next();
Student s = new Student(name, id);
classList.add(s);
}
input.close();
inFile = new File("C:/Users/mobin/Desktop/AB101.txt");
input = new Scanner(inFile);
while (input.hasNext()) {
int id = input.nextInt();
double mark = input.nextDouble();
Student s = findStudent(id, classList);
s.ab101mark = mark;
}
input.close();
inFile = new File("C:/Users/mobin/Desktop/AB102.txt");
input = new Scanner(inFile);
while (input.hasNext()) {
int id = input.nextInt();
double mark = input.nextDouble();
Student s = findStudent(id, classList);
s.ab102mark = mark;
}
input.close();
for (Student s : classList) {
s.setAggregate();
}
File outFile = new File("ABRankedList.txt");
PrintWriter output = new PrintWriter(outFile);
for (Student s : classList) s.printTranscript(output);
output.close();
}
}
In the listing, the results for each student should be separated by a row of “-“. The output should look like this:
------------------------------------------------------------
14321 Elizabeth
AB101: 80.2 70.4 73.3 AB102: 75.2 70.4 72.3 Overall mark: 72.8
14654 Charlie
AB101: 85.4 60.2 67.8 AB102: 80.4 65.2 71.3 Overall mark: 69.5
Upvotes: 0
Views: 86
Reputation: 897
You need to specify the full path when opening the file:
File inFile = new File("ABStudents.txt");
Should be something like:
File inFile = new File("C:/Users/myUserID/Desktop/ABStudents.txt");
There are ways to get the users home directory from system properties if it needs to be more flexible.
Upvotes: 0
Reputation: 1140
I suspect the issue is the files are saved on the desktop and the code is running in a different directory so the files aren't in your code's classpath. That's why Java can't find them.
If you change the pathname you pass to new File() to be the absolute path of the file, I believe it will work.
Upvotes: 1