0

I have code that basically reads the file and stores it in an object.

I'm having issues when trying to turn one of the String in the array into int/double etc.

I'm using a linked list to save the object.

I have tried debugging but I can't find the issue. The program runs fine and dandy until it reaches the changing of variables part. No matter what I do, what I change, or how I try to fix it the same exception occurs. And then sometimes when it works it doesn't save it.

private void readFile() {
        BufferedReader in;
        String line = "";

        try {
            in = new BufferedReader(new FileReader("Doggie.txt"));//file name

            try {
                line = in.readLine();
            } catch (Exception e) {
                line = null;
            }

            while (line != null) {
                String[] lineArray = line.split(",");

                String name = lineArray[0];
                String a = lineArray[1];
                String phone = lineArray[2];
                String location = lineArray[3];
                String p = lineArray[4];
                String id = lineArray[5];
                String dogName = lineArray[6];
                String breed = lineArray[7];
                String w = lineArray[8];
                String t = lineArray[9];
                String vet = lineArray[10];

                int age = Integer.parseInt(a);
                double payment = Double.parseDouble(p);
                double weight = Double.parseDouble(w);
                int times = Integer.parseInt(t);

                Dog doggo = new Dog(name, phone, location, payment, id, dogName, breed, age, weight, times, vet);

                dogsList.addNode(doggo);

                try {
                    line = in.readLine();
                } catch (Exception e) {
                    line = null;
                }
            }

            DogsLoaded = true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error opening files, please check the information provided", "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 2
    "An exception" really isn't as helpful as it could be. Have you considered logging the exception to see what it is? It might be valuable to include that in the question instead of making people wonder. – Dave Newton Nov 01 '21 at 17:28
  • You are currently "swallowing" the exception. That is you are hiding it and do nothing about it. Start by adding an `e.printStackTrace()`, when you catch it. This will allow you to see, where it is happening. My guess is that it is a NumberFormatException. – kgiannakakis Nov 01 '21 at 17:43
  • When I tested your code it worked, so check your input file and check your list and add the [stacktrace](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) to your question. – Alias Cartellano Nov 01 '21 at 18:27

1 Answers1

1

In the loop I do not see a repetition of readLine, a bug.

Evidently you need input validation and better error reporting. Data errors may be hard to find, so line content and line number might be benificial.

I added some validation, but it can be done nicer. I gave the cryptic Exception.getMessage (or getLocalizedMessage), as that at least will inform you to the cause.

    String message = "";
    int lineno = 0;
    try (BufferedReader in = Files.newBufferedReader(
            Paths.get("Doggie.txt"), Charset.defaultCharset())) {

        String line;
        while ((line = in.readLine())!= null) {
            ++lineno;
            String[] lineArray = line.split("\\s*,\\s*");

Above I allowed whitespace around the comma. This trims the fields, which otherwise would not be able to be converted to a number.

Checking the number of fields is important; for instance the name could contain a comma.

            if (lineArray.length != 11) {
                throw new IllegalArgumentException("Wrong number of fields: " + line);
            }

            String name = lineArray[0];
            String a = lineArray[1];
            String phone = lineArray[2];
            String location = lineArray[3];
            String p = lineArray[4];
            String id = lineArray[5];
            String dogName = lineArray[6];
            String breed = lineArray[7];
            String w = lineArray[8];
            String t = lineArray[9];
            String vet = lineArray[10];

The following conversions seem to go wrong, give a NumberFormatException probably. You certainly want to know the source.

            message = "Age wrong: " + lineno + ". " +line;
            int age = Integer.parseInt(a);
            message = "Payment wrong: " + lineno + ". " +line;
            double payment = Double.parseDouble(p);
            message = "Weight wrong: " + lineno + ". " +line;
            double weight = Double.parseDouble(w);
            message = "Times wrong: " + lineno + ". " +line;
            int times = Integer.parseInt(t);
            message = "";

            Dog doggo = new Dog(name, phone, location, payment,
                id, dogName, breed, age, weight, times, vet);

            dogsList.addNode(doggo);
        }

        DogsLoaded = true;
    } catch (Exception e) {
        if (message.isEmpty()) {
            message = e.getMessage()
        }
        JOptionPane.showMessageDialog(this, 
            "Error opening files: " + message,
            "Error", JOptionPane.ERROR_MESSAGE);
    }

Additionally I used Files with Paths and Path, which is the swiss knife utility to go with. The try-with-resources-syntax ensures that the file is closed also on exception or return.

Files has also functions for reading all lines, writing all lines, reading in a Stream of lines.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138