The following is the data that you can add to your input file (as an example). Notice that the first line is going to be a line representing your own hobbies. In my case, it is the Vitaly,table tennis,chess,hacking line. Your goal is to create a class called Student. Every Student will contain a name (String) and an ArrayList<String> storing hobbies. Then, you will add all those students from the file into an ArrayList<Student>, with each Student having a separate name and ArrayList of hobbies. Here is an example file containing students (the first line will always represent yourself). NOTE: eventually, we will have a different file containing all our real names and hobbies so that we could find out with how many people each of us share the same hobby. Vitaly,table tennis,chess,hacking Sean,cooking,guitar,rainbow six Nolan,gym,piano,reading,video games Jack,cooking,swimming,music Ray,piano,video games,volleyball Emily,crochet,drawing,gardening,tuba,violin Hudson,anime,video games,trumpet Matt,piano,Reading,video games,traveling Alex,swimming,video games,saxophone Roman,piano,dancing,art Teddy,chess,lifting,swimming Sarah,baking,reading,singing,theatre Maya,violin,knitting,reading,billiards Amy,art,gaming,guitar,table tennis Daniel,video games,tennis,soccer,biking,trumpet Derek,cooking,flute,gaming,swimming,table tennis Daisey,video games,guitar,cleaning,drawing,animated shows,reading,shopping Lily,flute,ocarina,video games,baking Stella,roller skating,sudoku,watching baseball,harp Sophie,viola,ukulele,piano,video games
时间: 2024-03-05 18:06:38 浏览: 61
I understand that you want me to create a class called Student and add all the students from the given file into an ArrayList<Student>, with each Student having a separate name and ArrayList of hobbies. Here's the code to achieve the same:
```
import java.io.*;
import java.util.*;
class Student {
private String name;
private ArrayList<String> hobbies;
public Student(String name, ArrayList<String> hobbies) {
this.name = name;
this.hobbies = hobbies;
}
public String getName() {
return name;
}
public ArrayList<String> getHobbies() {
return hobbies;
}
}
public class Main {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("students.txt"));
String[] firstLine = scanner.nextLine().split(",");
String myName = firstLine[0];
ArrayList<String> myHobbies = new ArrayList<>(Arrays.asList(Arrays.copyOfRange(firstLine, 1, firstLine.length)));
ArrayList<Student> students = new ArrayList<>();
students.add(new Student(myName, myHobbies));
while (scanner.hasNextLine()) {
String[] line = scanner.nextLine().split(",");
String name = line[0];
ArrayList<String> hobbies = new ArrayList<>(Arrays.asList(Arrays.copyOfRange(line, 1, line.length)));
students.add(new Student(name, hobbies));
}
scanner.close();
// Now, the students ArrayList contains all the students with their respective names and hobbies
// You can use this ArrayList to perform any further operations as needed
}
}
```
You can replace "students.txt" with the name of your input file containing the student data. Let me know if you have any further questions.
阅读全文