-1

I have a problem with maing java reading a file for me. I have a .txt-file with all the danish islands but somehow it will not display in the console no matter how i try.

This is the class with the main method. From here it reads the file and splits the lines in order to put the data into an ArrayList.

public class DanishIslandFileReader {

private File inFile;
private List<DanishIsland> islandList;

public DanishIslandFileReader(String fName) {
    inFile = new File(fName);
}

private void readFile() {
    islandList = new ArrayList<DanishIsland>();
    Scanner scan = null;
    try {
        scan = new Scanner(inFile);
    } catch (FileNotFoundException fnfe) {
        System.out.println(fnfe);
    }
    while (scan.hasNext()) {
        String line = scan.nextLine();
        String[] tokens = line.split(" ");

        String name = tokens[0];
        double circ = Double.parseDouble(tokens[1]);
        double area = Double.parseDouble(tokens[2]);
        int addr = Integer.parseInt(tokens[3]);
        int adkm = Integer.parseInt(tokens[4]);

        DanishIsland island = new DanishIsland(name, circ, area, addr, adkm);
        System.out.println(island.toString());
        islandList.add(island);
    }

    scan.close();
}

public List<?> getList() {
    return islandList;
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException {
    System.out.println(new File(".").getAbsolutePath());
//        DanishIslandFileReader fr = new DanishIslandFileReader("Islands punktum.txt");
    DanishIslandFileReader fr = new DanishIslandFileReader("Islands komma.txt");
    fr.readFile();

    System.out.println("Result:\n" + fr.getList());

}

}

When the line has been split it goes through another class which turns the data into a String and puts it into another Arraylist which will be printed in the consol.

public class DanishIsland {

private String name;
private double circumference;
private double area;
private int addresses;
private int addrPerKm2;

public DanishIsland(String name, double circumference, double area,
        int addresses, int addrPerKm2) {
    super();
    this.name = name;
    this.circumference = circumference;
    this.area = area;
    this.addresses = addresses;
    this.addrPerKm2 = addrPerKm2;
}

public String getName() {
    return name;
}

public double getCircumference() {
    return circumference;
}

public double getArea() {
    return area;
}

public int getAddresses() {
    return addresses;
}

public int getAddrPerKm2() {
    return addrPerKm2;
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append(name);
    builder.append("\t");
    builder.append(circumference);
    builder.append("\t");
    builder.append(area);
    builder.append("\t");
    builder.append(addresses);
    builder.append("\t");
    builder.append(addrPerKm2);
    builder.append("\n");
    return builder.toString();
}

}

I do not get any errors or exceptions and the program run till it thinks the list has been printed.

Problem is that the list is empty and I can't seem to make any sort of "sout" from the .txt-file. Whar am I doing wrong?

Lolland 298,388 1234,96 38919 32
Bornholm 108,047 588,025 27125 46
Falster 145,926 516,268 26654 52
Mors 139,254 361,745 12374 34
CasaRol
  • 1
  • 2

1 Answers1

0

Have you tried debugging it? I think it is not entering

while (scan.hasNext())

because scan.hasNext returns false. This could be a file permission issue.

Also this may be related: Why is hasNext() False, but hasNextLine() is True?

Canonip
  • 41
  • 6
  • It doesnt enter the while-loop. which means that it probably doesn't read from the file at all? – CasaRol Apr 12 '18 at 19:47
  • If it doesn’t enter the while-loop at all, try printing `scan.ioException()` before the loop. – VGR Apr 12 '18 at 21:43