#Im back, means I have more questions

1 messages · Page 1 of 1 (latest)

radiant hawk
#
package Exercise30_StoringRecords;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;

public class StoringRecords {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Filename:");
        String file = scan.nextLine();

        ArrayList<Person> records = readRecordsFromFile(file);
        System.out.println("Persons: " + records.size());
        System.out.println("Persons:");
        for (Person person : records) {
            System.out.println(person);

        }
    }

    public static ArrayList<Person> readRecordsFromFile(String file) {
        ArrayList<Person> persons = new ArrayList<>();

        try (Scanner scanner = new Scanner(Paths.get("data3.txt"))) {

            while (scanner.hasNextLine()) {
                Scanner line = scanner.nextLine();

                String[] parts = line.split(",");
                String name = parts[0];
                int age = Integer.valueOf(parts[1]);
            }
        }
        return persons;

    }
}

so I have to read a file and seperate the file and then like print it, why is the split for me red? is it because of the return? (the return was already there)

shrewd iglooBOT
#

<@&987246399047479336> please have a look, thanks.

shrewd iglooBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

ublic static void main(String[] args) {
ArrayList<String> records = new ArrayList<>();

    // Read records from file
    try (Scanner scanner = new Scanner(Paths.get("records.txt"))) {
        while (scanner.hasNextLine()) {
            String record = scanner.nextLine();
            records.add(record);
        }
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }

    // Print records
    for (String record : records) {
        System.out.println(record);
    }
}

}


This code reads records from a file called "records.txt" and stores them in an ArrayList. Then, it prints out the records.
radiant hawk
#

btw here are all my errors

#

oh forgot the catch