0

I write a code that reads a list of student grades from standard input line-by-line until there are no more lines to read or an empty line is encountered.However, I cannot exit the loop anyway. I tried to write
Scanner input = new Scanner(input.hasNext()); and else break but it is not working

public class NumInput {
  public static void main(String [] args) {
    float min = Float.MAX_VALUE;
    float max = Float.MIN_VALUE;
    float total=0;
    int count=0;
    Scanner input = new Scanner(System.in);
    while (input.hasNext()) {

      float val = input.nextFloat();


      if (val < min) {
          min = val;
      }
      if (val > max) {
         max = val;
      }
      count++; 
      total += val ;
    }
    float average = (float) total / count;
    System.out.println("min: " + min);
    System.out.println("max: " + max);
    System.out.println("The Average value is: " + average);
  }
}
merve
  • 73
  • 2
  • 12
  • 1
    Err... Java has `break`, you know... – fge Oct 23 '15 at 15:20
  • when I do this it also does not exit. else { break; } – merve Oct 23 '15 at 15:25
  • 1
    @SotiriosDelimanolis I don't think this is a duplicate of that question. It seems more like a duplicate of [this one](http://stackoverflow.com/questions/15847036/java-scanner-hasnext-doesnt-return-false). The problem isn't knowing how to exit; it's knowing what the condition to exit on is. – resueman Oct 23 '15 at 15:28
  • 1
    Vote to re-open if you'd like. Their breaking condition is _an empty line is encountered_. They can then figure out how to get that with a `Scanner`. – Sotirios Delimanolis Oct 23 '15 at 15:33

0 Answers0