samedi 7 mars 2015

Java - incrementing a counter once when within a range



I have a program that grabs a file containing boolean values, and I'm counting the number of false values until it reaches a true value. Then, when it encounters another false value it counts them again. I have a lower threshold and a higher threshold for the number of false values, and I need to increment a counter only once when within this range. This is not a homework assignment, by the way.



**private static int readBooleanValues(File textFile, int higher, int lower) throws FileNotFoundException {
Scanner scanner = new Scanner (textFile);
int counter = 0;
int secondaryCounter = 0;
while ( scanner.hasNext() ) {
String value = scanner.next();
if ( value.equalsIgnoreCase("false") ) {
counter++;
if ( counter >= lower && counter <= higher ) {
secondaryCounter++;
}
} else if ( value.equalsIgnoreCase("true") ) {
counter = 0;
}
}
return secondaryCounter;
}**



Aucun commentaire:

Enregistrer un commentaire