Home > Software design >  I have values from a text file that can only be counted if they are between 0 & 100, my code keeps s
I have values from a text file that can only be counted if they are between 0 & 100, my code keeps s

Time:10-10

I want to make it where the values I count from a text file can only be between 0 and 100, it can't be less than 0 or more than 100. The values in the text file are separated by spaces and sometimes lines ex: 57 59 38 60 49 24 60 39 I put in a if condition but it seems to be leaving out the first number even if that number is in the condition range, I assume it is because of my variable num, but I am not sure how to fix that. Here's my code:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class ExamStats {

    
    static final int A = 90,
                     B = 80,
                     C = 70,
                     D = 60;
    public static void main(String[] args) throws IOException {
        
        double minimumScore = 0;
        double maximumScore = 0;
        //total count of various grades
        int totalCount = 0;
        double average = 0;
        double sum = 0;
        
        
        Scanner input = new Scanner(System.in);
        //asks for file's name
        System.out.print("Input File's name: " );
        String inputFile = input.nextLine();
        //reads the files data
        File file = new File(inputFile);
        Scanner scanFile = new Scanner(file);

        int num = scanFile.nextInt();
    
    if(num >= 0 && num <= 100) {
        while(scanFile.hasNextInt()) {
        
        
            sum  = scanFile.nextInt();
            totalCount  ;   
        }
    }
        average = sum/totalCount;
        
        System.out.println("Number of scores: "   totalCount);
        System.out.printf("Average score: %.2f\n", average);
        scanFile.close();

CodePudding user response:

You need to move the condition inside the while loop.

This checks if the first number is inside the [0,100] range, and then executes some code:

int num = scanFile.nextInt();
if(!(num < 0 && num > 100)) {
  while(scanFile.hasNextInt()) {
    ...
    num = scanFile.nextInt();
  }
}

This checks if the current reading line is inside the [0,100] range, and only if it's true it executes some code:

while(scanFile.hasNextInt()) {
  int num = scanFile.nextInt();
  if(!(num < 0 && num > 100)) {
    ...
  }
}

If you want to check if all numbers are inside that range just use a boolean inside the 'if' code.

Also, some files have '\n' at the end. I usually use scanFile.readLine() and then parse that to int:

while(scanFile.hasNextLine()) {
  int num = Integer.parseInt(scanFile.readLine());
  if(!(num < 0 && num > 100)) {
    ...
  }
}

CodePudding user response:

I think you need something like this.

while (scanFile.hasNextInt()) {
    int num = scanFile.nextInt();
    if (num >= 0 && num <= 100) {
        sum  = num;
        totalCount  ;
    }
}

And the possible reason for earlier exit is if the first line in a file is less than zero or greater than 100. In that case it will never enter inside the then-block ( if(!(num < 0 && num > 100)){.


For scanning multiple values in lines. You should scan line by line (String). Then split each line into an array of numbers (split by space " "). And finally, each array element from the arrays turn into a number.

while (scanFile.hasNextInt()) {
    String line = scanFile.nextLine();
    String[] numbers = line.split(" ");

    for (String number : numbers) {
        int num = Integer.parseInt(number);

        if (num >= 0 && num <= 100) {
            sum  = num;
            totalCount  ;
        }
    }
}
  • Related