英文:
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("BloodInFile.txt"));
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("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;
}
答案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<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;
}
This way you should get the list with all of your numbers. Mine is with string, but should work with int.
专注分享java语言的经验与见解,让所有开发者获益!
评论