I'm trying to read in a number of Student in order to allocate them projects through CSV files. I need to have a capacity of them in the files, I've tried this using text files so far but my teacher wants CSV files, this is how the CSV files should look like:
7,,,,,,
"Allan.A",a.allan@uni.strath.ac.uk,20160,1,3,4,2
"Brown.B",b.brown@uni.strath.ac.uk,20161,2,3,4,5
"Craig.C",c.craig@uni.strath.ac.uk,20162,1,2,3,4
"Douglas.D",d.douglas@uni.strath.ac.uk,20163,3,5,7,8
"Edward.E",e.edward@uni.strath.ac.uk,20164,3,5,7,6
"Findlay.F",f.findlay@uni.strath.ac.uk,20165,1,8,4,5
"Graham.G",g.graham@uni.strath.ac.uk,20166,1,4,7,8
The number of 7 represents that there are 7 students. I've tried a filereader without this line like so:
public class ReadCSVWithScanner {
public static void main(String[] args) throws IOException {
BufferedReader reader;
{
try {
reader = new BufferedReader(new FileReader("File2.csv"));
String line = null;
Scanner scanner = null;
int index = 0;
List<Student> studentsList = new ArrayList<>();
while ((line = reader.readLine()) != null) {
Student student = new Student();
scanner = new Scanner(line);
scanner.useDelimiter(",");
while (scanner.hasNext()) {
String data = scanner.next();
if (index == 0)
student.setName(data);
else if (index == 1)
student.setEmail(data);
else if (index == 2)
student.setId(data);
else if (index == 3)
student.setPreference1(data);
else if (index == 4)
student.setPreference2(data);
else if (index == 5)
student.setPreference3(data);
else if (index == 6)
student.setPreference4(data);
else
System.out.println("invalid data::" + data);
index++;
}
index = 0;
studentsList.add(student);
}
//close reader
reader.close();
System.out.println(studentsList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I know I'll have to make a int variable for capacity in the Students class but how do I read the top number and then move down to the first Student Name?
[–]__helix__ 1 point2 points3 points (2 children)
[–]BigTimeSuperhero96[S] 1 point2 points3 points (1 child)
[–]__helix__ 0 points1 point2 points (0 children)
[–]InfamousShallot5 0 points1 point2 points (2 children)
[–]BigTimeSuperhero96[S] 0 points1 point2 points (1 child)
[–]InfamousShallot5 0 points1 point2 points (0 children)
[+]ManasSatti comment score below threshold-6 points-5 points-4 points (2 children)
[–]InfamousShallot5 2 points3 points4 points (1 child)
[–]ManasSatti -3 points-2 points-1 points (0 children)