-1

Straight to the point. I believe it's very easy to solve, but somehow I'm stuck. I have a class Uploader which is called from main class. This class streams a txt file with a student list and prints it out. However, I can't find a solution, how to store this txt file in the ArrayList and use it in other classes. File is sorted based on the Student class which overrides toString method to get the print output. I tried to use collect(collectors.toList()) method, but didn't work. I guess that I'm missing something obvious. Could anyone suggest anything ? Thanks.

Here is my code for Student class:

public class Student extends People {
private String collegeName;
private String studentType;
private final String studentID;

public Student(String firstName, String secondName, String telNo, String collegeName, String studentID, String studentType ) {
    super(firstName, secondName, telNo);
    this.studentID = studentID;
    this.collegeName = collegeName;
    this.studentType = studentType;

}

public String getStudentI() {
    String studentDet = getPeople() + collegeName + " " + studentType + " Student";
    return studentDet;


}
@Override
public String toString() {
    return "Student [name= " + firstName + ", second Name= " + secondName + " Telephone number= " + telNo + "college name= " + collegeName + " Student ID " + studentID + " Student Type= " + studentType + "]";
}

}

And here is Uploader Class:

public class Uploader {

private String fileName = null;


private void getPath() {

    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    setFileName(f.getAbsolutePath());
    System.out.println(getFileName());

}
void Uploader() {

    getPath();
    Path path = Paths.get(getFileName());

    try (Stream<String> lines = Files.lines(path)) {
        lines
                .flatMap(Uploader::lineToPerson)
                .forEach(System.out::println);

    } catch (IOException ioe) {

        ioe.printStackTrace();


    }
}


public static Stream<Student> lineToPerson(String line) {

    try {
        String[] elements = line.split(",");
        String firstName = elements[0];
        String secondName = elements[1];
        String telNo = elements[2];
        String collegeName = elements[3];
        String studentID = elements[4];
        String studentType = elements[5];


        Student p = new Student(firstName, secondName, telNo, collegeName, studentID, studentType);

        return Stream.of(p);


    } catch (Exception e) {

    }
    return Stream.empty();
}


public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

}

  • 1
    [edit] your question and post sample data from the _txt_ file containing the student details. – Abra May 21 '20 at 11:59

1 Answers1

1

There is no need to use a flatMap() here. Simple map transformation can be used to convert line to Student object and collect them in a list.

        List<Person> personList = new ArrayList();
        try (Stream<String> lines = Files.lines(path)) {
            personList = lines
                    .map(this::lineToPerson)
                    .collect(Collectors.toList());   
        } catch (IOException ioe) {    
            ioe.printStackTrace();
        }


        public Student lineToPerson(String line) {
            String[] elements = line.split(",");
            String firstName = elements[0];
            String secondName = elements[1];
            String telNo = elements[2];
            String collegeName = elements[3];
            String studentID = elements[4];
            String studentType = elements[5];

            return new Student(firstName, secondName, telNo, collegeName, studentID, studentType);

        }
Kumar V
  • 1,570
  • 1
  • 12
  • 19
  • Thank you, this is exactly what I needed. You saved me hours of searching. It shows me how much more I need to learn. Thanks again ! – Adam Rudziński May 21 '20 at 12:33
  • Glad that it solved your problem. Happy learning! – Kumar V May 21 '20 at 12:41
  • What is the difference between map and flatmap in simple layman terms? – John Doe May 22 '20 at 05:38
  • map - apply transformation for collection of items (Collection) and collect them in another collection. flatmap - apply transformation for collection of collection of items (Collection) and collect them in single collection (Collection). – Kumar V May 22 '20 at 12:22