在Java中读取txt文件时,程序跳过了第一行。

huangapple 未分类评论53阅读模式
英文:

Reading txt file in java, program skipping the first line

问题

static int getPints(int[] pints) throws IOException {
    Scanner inputFile = new Scanner(new File("BloodInFile.txt"));
    int count = 0;
    while (inputFile.hasNext()) {
        pints[count] = inputFile.nextInt();
        count++;
    }
    inputFile.close();
    return pints[count - 1];
}

static double getAverage(int pints[], int MAX_HOURS) {
    int counter;
    double totalPints = 0; // Changed initial value to 0
    double averagePints;
    for (counter = 0; counter < MAX_HOURS; counter++) { // Removed -1 from loop condition
        System.out.println("pints:" + pints[counter]);
        totalPints = totalPints + pints[counter];
    }
    averagePints = totalPints / MAX_HOURS; // Removed -2 from denominator
    System.out.println("Total pints: " + totalPints);
    return averagePints;
}

static double getHigh(int pints[], int MAX_HOURS) {
    int index;
    double highest = pints[0]; // Changed index to 0
    for (index = 1; index < MAX_HOURS; index++) { // Removed -1 from loop condition
        if (pints[index] > highest) {
            highest = pints[index];
        }
    }
    return highest;
}

static double getLow(int pints[], int MAX_HOURS) {
    int index;
    double lowest = pints[0]; // Changed index to 0
    for (index = 1; index < MAX_HOURS; index++) { // Removed -1 from loop condition
        if (pints[index] < lowest) {
            lowest = pints[index];
        }
    }
    return lowest;
}
英文:

My program is skipping the first line of my .txt file. Here is where my code is supposed to read from the .txt file.

static int getPints(int[] pints) throws IOException {
    Scanner inputFile = new Scanner(new File(&quot;BloodInFile.txt&quot;));
    int count = 0;
    while (inputFile.hasNext()) {
        pints[count] = inputFile.nextInt();
        count++;
    }
    return pints[count];
}

It should be reading 7 integers, but I only get 6 of them to be read.
The rest of the code works, but I am not sure how to get it to read the first integer.

This is the rest of my code:

static int getPints(int[] pints) throws IOException {
    Scanner inputFile = new Scanner(new File(&quot;BloodInFile.txt&quot;));
    int count = 0;
    while (inputFile.hasNext()) {
        pints[count] = inputFile.nextInt();
        count++;
    }
    inputFile.close();
    return pints[count];
}

static double getAverage(int pints[], int MAX_HOURS) {
    int counter;
    double totalPints = 1;
    double averagePints;
    for (counter = 1; counter &lt; MAX_HOURS - 1; counter++) {
        System.out.println(&quot;pints:&quot; + pints[counter]);
        totalPints = totalPints + pints[counter];
    }
    averagePints = totalPints / (MAX_HOURS - 2);
    System.out.println(&quot;Total pints: &quot; + totalPints);
    return averagePints;
}

static double getHigh(int pints[], int MAX_HOURS) {
    int index = 1;
    double highest = pints[index];
    for (index = 1; index &lt; MAX_HOURS - 1; index++) {
        if (pints[index] &gt; highest) {
            highest = pints[index];
        }
    }
    return highest;
}

static double getLow(int pints[], int MAX_HOURS) {
    int index = 1;
    double lowest = pints[index];
    for (index = 1; index &lt; MAX_HOURS - 1; index++) {
        if (pints[index] &lt; lowest) {
            lowest = pints[index];
        }
    }
    return lowest;
}

答案1

得分: 0

以下是翻译好的内容:

我曾经就像这样做过一次,也许这对你也有用

public static List<String> get16TeamsFromTxt() throws IOException {
    Path currentRelativePath = Paths.get("");
    String s1 = currentRelativePath.toAbsolutePath().normalize().toString();

    Scanner s = new Scanner(new File(s1+"/16.txt"));
    ArrayList<String> list = new ArrayList<String>();
    while (s.hasNextLine()){
        list.add(s.nextLine());
    }
    s.close();

    return list;
}

这样您应该获得包含所有数字的列表我的是字符串但对于整数也应该适用
英文:

I did it once just like this, maybe that works for you too

public static List&lt;String&gt; get16TeamsFromTxt() throws IOException {
    Path currentRelativePath = Paths.get(&quot;&quot;);
    String s1 = currentRelativePath.toAbsolutePath().normalize().toString();

    Scanner s = new Scanner(new File(s1+&quot;/16.txt&quot;));
    ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
    while (s.hasNextLine()){
        list.add(s.nextLine());
    }
    s.close();

    return list;

}

This way you should get the list with all of your numbers. Mine is with string, but should work with int.

huangapple
  • 本文由 发表于 2020年5月30日 03:35:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/62093462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定