简体   繁体   中英

Why is my program reading one less line than there actually is? And why is my array taking in only ones?

In my high school comp sci class I have to read a text file with marks and then create an array with those marks in them (so I can manipulate them later). When I try and read the number of lines in the program it reads one less than there is, and when I output the array it consists of only "1.00" written to the amount of lines it has counted (which is incorrect).

import java.awt.*;
import java.io.*;
import hsa.Console;

public class Assignment3Q3
{
    static Console c;
    public static void main (String[] args) throws IOException
    {
        c = new Console ();

        BufferedReader input = new BufferedReader (new FileReader ("marks.txt"));
        String mark = input.readLine ();
        int lines = 0;
        while (input.readLine () != null)
            lines++;
        input.close ();

        c.println (lines);

        double[] marks = new double [lines];
        int count = 0;

        BufferedReader input1 = new BufferedReader (new FileReader ("marks.txt"));


        while (input1.readLine () != null)
        {
            marks [count] = Double.parseDouble (mark);

            count += 1;
            if (count == lines)
            {
                break;
            }
        }

        for (int x = 0 ; x < lines ; x++)
        {
            c.println (marks [x]);
        }
    }
}

In your second while loop, you are always assigning the parsed version of mark variable to the marks array elements. But you have only set mark variable once in your code, which is the first line of your file.

Anyway without reading the file twice (once to get the number of lines and then to store the actual line content), you can do this in a single read cycle by using a List instead of an array.

try (BufferedReader input = new BufferedReader (new FileReader("src/marks.txt"))) {
    List<Double> marks = new ArrayList<>();
    String line;
    while ((line = input.readLine()) != null) {
        marks.add(Double.parseDouble(line));
    }
    System.out.println(marks);
} catch (IOException e) {
    e.printStackTrace();
}

In case you really want to get these marks to an array, you can onvert the above list into an array as follows.

Double[] marksArray = marks.toArray(new Double[marks.size()]);

Also as I have done in the above code snippet, better to use try with resources approach when you create AutoCloseable resources such as BufferedReader or FileReader . Then you don't have to close them explicitly in your code.

Why this separation in two steps at all? This is error prone. No values in the marks -array above the current line- count are accessed. So store the doubles in a dynamicly growing ArrayList<Double> instead and do the job in one step.

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