0

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;
}
  • Could you also share the content of your file? – Balastrong May 29 '20 at 19:37
  • With `pints[count]` you'll get only the last read element anyway. – Balastrong May 29 '20 at 19:40
  • Nah, they'll get one after that, since `count++` is done after reading into index `count`. So next thing should be an `ArrayIndexOutOfBoundsException`, but I'm guessing the array size is prepared beforehand 'to be safe'. – pafau k. May 29 '20 at 19:43

1 Answers1

0

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.