简体   繁体   中英

BufferedReader issue: String and Numeric values from a .txt are being retrieved as null and zeros

I have tried to get string and numeric values from a text file using BufferedReader. Apparently, it is recognizing the number of records (lines) of the text file, but the values are not being retrieved as they are. String ones are being placed as "null" and double as zeros. I am kind of new in Java and I would like to know the possible reason of this output and also how could I solve it.

在此处输入图像描述

I tried to adapt the results to an arraylist as part of a table visualization, however after some tries it was not possible solve the problem:

private ArrayList<ListClasses> seeListe() {
    ArrayList<ListClasses> list = new ArrayList<>();
    
    File file = new File("C:/file/to/path/file.txt");
    BufferedReader br = null;

    try {               
        FileReader fr = new FileReader(file);
        BufferedReader breader = new BufferedReader(fr);
        String line = breader.readLine();

        while (line != null) {
            list.add(new ListClasses());
            line = breader.readLine();
        }           
    } catch (FileNotFoundException e) {
        System.out.println("File not found: " + file.toString());
    } catch (IOException e) {
        System.out.println("Unable to read file: " + file.toString());
    }
    finally {
        try {
            br.close();
        } catch (IOException e) {
            System.out.println("Unable to close file: " + file.toString());
        }
        catch(NullPointerException ex) {
        }
    }
    
    return list;
}

The records in you file should really represent one object so your class name shouldn't be a plural, but if your ctor is right, you can just do:

List<ListClasses> data = Files.lines(Paths.get(pathToDataFile)).map(ListClasses::new).collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM