0

I have a file called Vehicle.txt contains example:

(123,123,123,123,123
456,4,456,456,456)

The output only executed the first line will be add into the arraylist. I just wonder how can I read for the second line.

    static void readVehicleFile() {
        Vehicle vehicle;
        try {

            input = new Scanner(new File("Vehicle.txt"));
            StringTokenizer tokenizer =  new StringTokenizer(input.next(),",");
            while (input.hasNextLine() && tokenizer.hasMoreTokens()) {
                vehicle = new Vehicle();
                vehicle.setPlateNumber(tokenizer.nextToken());
                vehicle.setNumOfYear(Integer.parseInt(tokenizer.nextToken()));
                vehicle.setMake(tokenizer.nextToken());
                vehicle.setModel(tokenizer.nextToken());
                vehicle.setEngineCapacity(Integer.parseInt(tokenizer.nextToken()));
                vehicle.setOwnerID(Integer.parseInt(tokenizer.nextToken()));


                vehicleList.add(vehicle);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

2 Answers 2

1

Instead of using Scanner, I recommend using FileReader and a BufferedReader. A BufferedReader has a method called readLine() that reads the next line.

private static String[] getCSV(File path) {
        if (!path.exists())
            return null;
        try {
            ArrayList<String> array = new ArrayList<>();
            FileReader reader= new FileReader(path);
            BufferedReader br = new BufferedReader(reader);
            String line;
            while((line = br.readLine()) != null){
                String[] csv = line.split(",");
                array.addAll(Arrays.asList(csv));
            }
            br.close();
            reader.close();
            return Arrays.copyOf(array.toArray(), array.size(), String[].class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

This is a method I made a while ago that returns an array of String with the comma separated values of your file. You can add a tiny modification to remove the parenthesis from the first and last values. Then, you can just use the values in that String array to set your vehicle methods.

The Exception handling should be a bit more polished, if there's an error reading the file the readers will not close, resulting in a memory leak. I hope this at least helps you read your values.

Note: If you want a walkthrough of the method, add a comment and I'll explain further.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use java.nio.file.Files.readline

import java.nio.file.Files;
import java.nio.file.Paths;    
...
List<String> lines = Files.readAllLines(Paths.get("/tmp/readlines.txt"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.