Reputation: 67
i have a txt of marks for students, every line being a new student. i want to add all the first marks of every student to an array, then all the second marks to a different array etc. every array is the module, i then have methods to find the mean and median etc of these module marks, but im struggling reading every line then adding the first values of every line etc.
public interface StatCalculator {
double[] CE1014FY = {};
double[] CE1014SP = {};
double[] CE1414AU = {};
double[] CE1414FY = {};
double[] CE1424AU = {};
double[] CE1424FY = {};
double[] CE1514AU = {};
double[] CE1524SP = {};
double[] CE1534AU = {};
double[] CE1544SP = {};
double[] CE1554SP = {};
double[] CE1614AU = {};
double[] CE1624SP = {};
double[] CE1634AU = {};
double[] CE1644SP = {};
static void get(){
try {
File file = new File("M:\\Documents\\ce201\\subversion\\resources\\marks.txt");
Scanner scanner = new Scanner(file);
for every line
CE1014FY.add(line[0]; //i want something like this
CE1014SP.add(line[1]
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
static double mean(double[] numbers){
double sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
System.out.println("mean: "+sum/numbers.length);
return sum / numbers.length;
}
public static double median(double[] numbers) {
Arrays.sort(numbers);
int middle = numbers.length/2;
if (numbers.length%2 == 1) {
System.out.println("median: "+numbers[middle]);
return numbers[middle];
} else {
System.out.println("median: "+(numbers[middle-1] + numbers[middle]) / 2.0);
return (numbers[middle-1] + numbers[middle]) / 2.0;
}
}
public static double lowerQ(double[] numbers) {
Arrays.sort(numbers);
int lqpos = (numbers.length+1)/4;
int lq= (int) (int) numbers[lqpos];
System.out.println("lower q: "+lq);
return lq;
}
public static double upperQ(double[] numbers) {
Arrays.sort(numbers);
int uqpos = 3*(numbers.length+1)/4;
int uq= (int) (int) numbers[uqpos];
System.out.println("upper q: "+uq);
return uq;
}
public static void main(String[] args) {
get();
mean(CE1014FY);
median(CE1014FY);
upperQ(CE1014FY);
lowerQ(CE1014FY);
}
}
then the txt{
63,-1,-1,76,-1,-1,82,85,84,57,67,73,-1,-1,-1,-1,73
62,-1,-1,60,-1,-1,89,76,79,53,55,77,-1,-1,-1,-1,69
60,-1,-1,42,-1,-1,37,32,67,-1,44,56,37,-1,-1,-1,47
53,-1,-1,88,-1,-1,75,68,69,58,64,75,-1,-1,-1,-1,69
72,-1,-1,64,-1,-1,39,55,74,56,78,64,-1,-1,-1,-1,63
50,-1,-1,30,-1,-1,19,20,35,19,7,34,-1,-1,-1,-1,27
}
Upvotes: 3
Views: 105
Reputation: 1455
I would use lists instead of arrays, because arrays doesn't have an "Add2 method, you should also use BufferedReader
object so you can read your text file line by line, considering each line is a mark you can use a counter for know in which line are you for know which list method you have to store it. So your get method should look like this:
static void get(){
try {
File file = new File("M:\\Documents\\ce201\\subversion\\resources\\marks.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int count = 1;
while((line = br.readLine()) != null)
{
switch(count)
{
case 1:
CE1014FY.add(Double.parseDouble(line));
break;
case 2:
CE1014SP.add(Double.parseDouble(line));
break;
case 3:
CE1414AU.add(Double.parseDouble(line));
break;
}
count++;
}
}
I just see the format of your file marks.txt, in this case if every line is separated by "," you should do an split and then in the array object resulted you every index correspond with a list in this case, your get method
static void get(){
try {
File file = new File("M:\\Documents\\ce201\\subversion\\resources\\marks.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line = br.readLine()) != null)
{
String[] lineSplitted = line.split(",");
for(int i=0; i<lineSplitted.length; i++)
{
switch(i)
{
case 0:
CE1014FY.add(Double.parseDouble(lineSplitted[i]));
break;
case 1:
CE1014SP.add(Double.parseDouble(lineSplitted[i]));
break;
case 2:
CE1414AU.add(Double.parseDouble(lineSplitted[i]));
break;
}
}
}
}
Upvotes: 1
Reputation: 690
First off, read your file into an array of lines. Use this method from this SO question:
public String[] readLines(String filename) throws IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
Second, split a coma-separated line into its constituents using this SO question:
String[] grades = line.split(",")
To avoid repeating your tables all over the place, use an enum
:
public enum Course {
CE1014FY, CE1014SP, CE1414AU, CE1414FY, CE1424AU, CE1424FY, CE1514AU,
CE1524SP, CE1534AU, CE1544SP, CE1554SP, CE1614AU, CE1624SP, CE1634AU, CE1644SP;
}
This makes it easy to not repeat your code.
We now have everything we need:
static Map<Courses, double[]> grades = new HashMap<>();
static void get(){
String[] lines = readLines("M:\\Documents\\ce201\\subversion\\resources\\marks.txt");
// Init the grades map
for(Course course: Course.values()) {
grades.put(course, new double[lines.length]);
}
// Fill the grades map
int lineNum = 0;
for(String line: lines){
String[] grades = line.split(",");
int courseNum = 0;
for(Course course: Course.values()) {
try{
grades.get(course)[lineNum] = Double.parseDouble(grades[courseNum++]);
} catch(NumberFormatException nfe){
grades.get(course)[lineNum] = -1;
System.out.println("Error reading grade for course " + course + ", line " + lineNum + " : " + grades[courseNum-1]);
}
}
}
}
You can then get any course grades using:
double[] gradesCE1414AU = grades.get(course.CE1414AU);
Edit: Seeing as your data does not have student name, I agree with using Arrays everywhere, because their staticity will protect the ordering, which is the only guarantee that your data makes sense. otherwise I would rather have (as usual) a List
of Student
Object with each their own Map<Course, Double> myGrades
field. Much more OOP.
Upvotes: 0