gpt4 book ai didi

java - 在java中读取txt文件,程序跳过第一行

转载 作者:行者123 更新时间:2023-12-01 16:28:45 30 4
gpt4 key购买 nike

我的程序正在跳过 .txt 文件的第一行。这是我的代码应该从 .txt 文件读取的位置。

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++;
}
return pints[count];
}

它应该读取 7 个整数,但我只读取了其中 6 个。其余代码有效,但我不确定如何让它读取第一个整数。

这是我的其余代码:

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];
}

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

static double getHigh(int pints[], int MAX_HOURS) {
int index = 1;
double highest = pints[index];
for (index = 1; index < MAX_HOURS - 1; index++) {
if (pints[index] > 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 < MAX_HOURS - 1; index++) {
if (pints[index] < lowest) {
lowest = pints[index];
}
}
return lowest;
}

最佳答案

我就这样做过一次,也许这也适合你

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;

}

这样您就可以获得包含所有号码的列表。我的是字符串,但应该与整数一起使用。

关于java - 在java中读取txt文件,程序跳过第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62093462/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com