I´m working with text file reads in Java, and I need to store the content of this text files inside of arrays, and the pass it as references for instance a new Class.
This is the structure of my txt file:
OXFORD
Software Engineer, Administracion, Laws
PRINCETON
Maths, Economy, Sociology
This is the class who have the structure of the file:
public class University {
private String name;
private String[] careers;
public University() {
}
public University(String name, String[] careers) {
this.name = name;
this.careers = careers;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getCareers() {
return careers;
}
public void setCareers(String[] careers) {
this.careers = careers;
}
This is the student class, who have a university and career as attribute from the University class:
public class Student {
private String name;
private University university;
private String career;
public Student() {
}
public Student(String name, University university, String career) {
this.name = name;
this.university = university;
this.career = career
}
public String getName(){
return name;
}
public string setName(String name){
this.name = name;
}
public University getUniversity(){
return university;
}
public University setUniversity(University university){
this.university = university;
}
}
As you can see, my careers attribute is an array. I want store the careers and university from the text file in my student careers array. And to do something like this:
Sample input:
System.out.println("Greetings student, to what career you wanna set up to");
//here I wanna type the name of the career from the careers list from the text file
And then have something like this:
Sample expected output:
System.out.println("Congrats new student, your new career is" + career);
This is my main method where I am reading the text file and printing it:
public static void readFiles() throws FileNotFoundException, IOException {
BufferedReader readBuffer = new BufferedReader(new FileReader("C:\\Users\\Roberto\\Documents\\NetBeansProjects\\sistemaBecas\\src\\ficheros\\Universidad.txt"));
String docLine;
ArrayList<String> registerList = new ArrayList<String>();
while ((docLine = readBuffer.readLine()) != null) {
registerList.add(docLine);
}
String[] arrayDocument = registerList.toArray(new String[0]);
for (int i = 0; i < arrayDocument.length; i++) {
System.out.println(Array.get(arrayDocument,i));
}
}
How could I do this?
Try this.
String file = "C:\\Users\\Roberto\\Documents\\NetBeansProjects\\sistemaBecas\\src\\ficheros\\Universidad.txt";
List<String> lines = Files.readAllLines(Paths.get(file));
List<University> universities = new ArrayList<>();
for (int i = 0; i < lines.size(); i += 2)
universities.add(new University(lines.get(i), lines.get(i + 1).split("\\s*,\\s*")));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.