Reputation: 557
So I have a program where I create new Student, add grades to that student, add the name of the class from which the student got thoes grades and then add the Student to my students list.
As there are many different classes, it tends to happen that the same student goes to multiple classes and has grades in all of them.
But this will make my program still create a new Student, even tough it allready exists.
This is how the code looks for creating a student right now:
List<Student> students = new ArrayList<>();
for(Path adress:files){
BufferedReader textIn = new BufferedReader(new FileReader(adress.toString()));
String line;
while((line = textIn.readLine()) != null){
String[] readContent = line.split(" ");
System.out.println(students.size());
for(int i = 0; i < students.size(); i++){
if(students.get(i).askName().equals(readContent[0])){
//something i do when Student exists
}else{
Student student = new Student(readContent[0],adress.toString());
for(int y = 1;y < readContent.length; y++){
student.addGrade(readContent[y]);
}
students.add(student);
}
}
}
}
As you can see I wanted to solve the problem of multiple Students with the same name by first going through my students list and seeing if Student.get(i).askNAme()
equals to any from my students list. But this creates a situation where the loop will never start.
How would you solve this in a nice manner? I don't think adding one empty student just for this at start would be really okay.
The whole Main class:
package files;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
Path lessons = Paths.get("../text/students");
FileVisitor file = new FileVisitor();
Files.walkFileTree(lessons, file);
List<Path> files = file.askFiles();
List<Student> students = new ArrayList<>();
for(Path adress:files){
BufferedReader textIn = new BufferedReader(new FileReader(adress.toString()));
String line;
while((line = textIn.readLine()) != null){
String[] readContent = line.split(" ");
System.out.println(students.size());
for(int i = 0; i < students.size(); i++){
if(students.get(i).askName().equals(readContent[0])){
}else{
Student student = new Student(readContent[0],adress.toString());
for(int y = 1;y < readContent.length; y++){
student.addGrade(readContent[y]);
}
students.add(student);
}
}
}
}
PrintWriter output = new PrintWriter(new FileWriter("../text/opilased_out"));
for(Student i:students){
output.printf("Nimi: %s\t|\tKeskmine hinne: %f\t|\tAine: %s\n", i.askName(), i.askMedian(), i.askSubject());
}
output.close();
}
}
Upvotes: 0
Views: 97
Reputation: 6230
Something like this should work as the inner most loop
boolean found = false;
for(int i = 0; i < students.size() && !found; i++){
if(students.get(i).askName().equals(readContent[0])){
//something i do when Student exists
found = true;
}
}
if(!found){
Student student = new Student(readContent[0],adress.toString());
for(int i = 1; i < readContent.length; i++){
student.addGrade(readContent[i]);
}
students.add(student);
}
Upvotes: 1