Чтение строк, затем разделение на запятую и разделение на точку с запятой, где это необходимо.
Не используйте Scanner
для чтения строки файла.
try (BufferedReader in = Files.newBufferedReader(Paths.get("test.txt"))) {
for (String line; (line = in.readLine()) != null; ) {
String[] fields = line.split(",");
String name = (fields.length >= 1 ? fields[0] : "");
String lastname = (fields.length >= 2 ? fields[1] : "");
String email = (fields.length >= 3 ? fields[2] : "");
String[] hobbies = (fields.length >= 4 ? fields[3].split(";") : new String[0]);
String[] activities = (fields.length >= 5 ? fields[4].split(";") : new String[0]);
System.out.println("name=" + name +
", lastname=" + lastname +
", email=" + email +
", hobbies=" + Arrays.toString(hobbies) +
", activities=" + Arrays.toString(activities));
}
}
test.txt
name,lastname,email,hobbies1;hobbies2...hobbiesN,activity1;activity2...activityN
name2,lastname2,email
Выход
name=name, lastname=lastname, email=email, hobbies=[hobbies1, hobbies2...hobbiesN], activities=[activity1, activity2...activityN]
name=name2, lastname=lastname2, email=email, hobbies=[], activities=[]