-1

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

2 Answers2

2

Instead of while(input.hasNext());, try while(input.hasNextFloat()); if it's always going to be a float type.

Also, while(input.hasNextFloat()); will continue reading the user's input until a non float value (or non int value) is entered. So you can enter 1 and 2 and finally q, then because q is not an float/int it'll exit the loop.

A more specific way of solving this would be to do the following:

while(input.hasNextLine()) {
    String command = input.nextLine();
    if(command.equals("")) {
        System.out.println("breaking out of the loop");
        break;
    }
    // rest of the code but make sure to use Float.parseFloat() on the `command`;
}

This documentation has good examples as well as explanation for hasNextFloat(), hasNextLine(), and parseFloat(): http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

sparkhee93
  • 1,381
  • 3
  • 21
  • 30
  • What should I write command= Float.parseFloat(); in paranthesis – merve Oct 23 '15 at 18:27
  • You want to put `command` into the `parseFloat()` parameter and it will parse `command` (a `String` type) into a `float` and return that value. So you would type something like `float f = Float.parseFloat(command);` – sparkhee93 Oct 23 '15 at 18:29
  • When I do this, `Exception in thread "main" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at sun.misc.FloatingDecimal.parseFloat(Unknown Source) at java.lang.Float.parseFloat(Unknown Source) at NumInput.main(NumInput.java:18)` I get result after 3 inputs – merve Oct 23 '15 at 18:33
  • `while (input.hasNextLine()) { String command = input.nextLine(); if(command.equals(" ")) { System.out.println("breaking out of the loop"); break; } float val = input.nextFloat(); float f = Float.parseFloat(command); ` – merve Oct 23 '15 at 18:38
  • Whenever you update your code, try to update on your actual post so that others can see. But just remove the last two lines and replace it with `float val = Float.parseFloat(command);`. Not only that but your `if(command.equals(" "))` is only checking to see if there is a space character entered. If that's the desired check, then you can leave it but it doesn't really match with the description on your post so just pointing that out. – sparkhee93 Oct 23 '15 at 18:43
0

To check empty line you can use nextLine instead of nextFloat then compare with "", here is working code

import java.util.Scanner;

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);
    String str;
    while (input.hasNextLine()) { 

       str = input.nextLine();
       if(str.equals(""))   //Check if it is empty line then break
           break;
      float val = Float.parseFloat(str);  //Convert string to float


      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);
  }
}
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42