简体   繁体   中英

Java counter issue

I am trying to write a program that reads values from a text file and checks how many values are within certain value ranges. The point is to eventually have the program print to the console the total number of values for each range. I have had it where it can do this 1 value at a time but then it would reset with every new int. In it's current it would simply repeat the first number over and over. To reiterate, I am trying to get it to report once it sorts through the values in the text file the amount of times a value was within the number ranges.

Here is my code.

package lab2;

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

public class Lab2 {

    public static void main(String[] args) throws FileNotFoundException {

        File file = new File("lab2_input.txt");
        Scanner scan = new Scanner(file);

        int counter1 = 0;
        int counter2 = 0;
        int counter3 = 0;
        int counter4 = 0;
        int counter5 = 0;
        int counter6 = 0;
        int counter7 = 0;
        int counter8 = 0;

        while(scan.hasNextLine()) {

            int number = scan.nextInt();
            System.out.println(number);

            if (number <= 24){
                counter1++;
            }
            if (25 <= number && number <= 49){
                counter2++;
            }
            if (50 <= number && number <= 74){
                counter3++;
            }
            if (75 <= number && number <= 99){
                counter4++;
            }
            if (100 <= number && number <= 124){
                counter5++;
            }
            if (125 <= number && number <= 149){
                counter6++;
            }
            if (150 <= number && number <= 174){
                counter7++;
            }
            if (175<= number && number >= 200){
                counter8++;
            }

        }

        System.out.println(counter1);
        System.out.println(counter2);
        System.out.println(counter3);
        System.out.println(counter4);
        System.out.println(counter5);
        System.out.println(counter6);
        System.out.println(counter7);
        System.out.println(counter8);
    }

}

I think you mean to check for example range from 25 to 49, then instead of:

if (25 <= number && number <= 49){
    counter2++;
}

you should write:

if (25 >= number && number <= 49){
    counter2++;
}

Same thing about other checks.

PS: I would suggest making counter an array and increment the elements based on calculated index, because the ranges are always 25: var index = number / 25 .

That looks like a lot of manual counter keeping; your desired intervals are regularly separated at twenty-five, so I would use an array. Additionally, use try-with-Resources to avoid leaking file handles. And use Scanner.hasNextInt() with Scanner.nextInt() (not Scanner.hasNextLine() ). Finally, I would add more descriptive messaging around the counts and their ranges. Like,

File file = new File("lab2_input.txt");
int[] counters = new int[9];
try (Scanner scan = new Scanner(file)) {
    while (scan.hasNextInt()) {
        int v = scan.nextInt();
        if (v >= 0 && v <= 200) {
            counters[v / 25]++;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
for (int i = 0; i < counters.length; i++) {
    System.out.printf("The range %d to %d had %d values%n", i * 25, 
            ((i + 1) * 25) - 1, counters[i]);
}

You can use else-if blocks instead of multiple if blocks and get rid of the lower bound validation. This would make some performance improvement as well if you are dealing with a large list of numbers.

    if (number <= 24){
        counter1++;
    } else if (number <= 49){
        counter2++;
    } else if (number <= 74){
        counter3++;
    } else if (number <= 99){
        counter4++;
    } else if (number <= 124){
        counter5++;
    } else if (number <= 149){
        counter6++;
    } else if (number <= 174){
        counter7++;
    } else if (number <= 200){
        counter8++;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM