0

So I've been working on an assignment for school. It says to make a program that does the following: 1. Prompt user for input 2. Take input from user three times, store as floats 3. Pass variables to method minimum() which uses Math.min() to calculate the minimum. 4. Print the result 5. Prompt user again and loop until EOF is received

Well, I did that but it didn't feel like a challenge. I've modified the code so that it keeps prompting the user for input and adding elements to an ArrayList which gets passed to minimum() which returns a float that gets printed, then the program should prompt the user for input once more and it should loop again until an EOF is received.

// imports
import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;

public class FindMin{
   public static void main(String args[]){
      // define vars
      float x;
      int iter = 1;

      // create new instance of Scanner called input
      Scanner input = new Scanner(System.in);
      // create new instance of ArrayList called nums with Float elements
      ArrayList<Float> nums = new ArrayList<Float>();

      // print the instructions and prompt the user for a number
      System.out.printf("%s\n   %s\n   %s\n",
      "Type the end-of-file indicator to terminate",
      "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
      "On Windows type <ctrl> z then press Enter");
      System.out.print("Enter a number: ");

      // loop until EOF, then quit
      while(input.hasNext()){ // get user input and exit if input is EOF
         // assign input to x, add x as a new element of nums, then prompt user
         x = input.nextFloat();
         nums.add(x);
         iter++;
         System.out.print("Enter a number: ");
         // loop until EOF, then calculate min
         while(input.hasNext()){
            x = input.nextFloat();
            nums.add(x);
            iter++;
            System.out.print("Enter a number: ");
         } // end while
         // calculate min and set iter back to 1
         System.out.printf("\n Minimum is: %f\n\n", minimum(nums));
         iter=1;
         // re-prompt user
         System.out.printf("%s\n   %s\n   %s\n",
         "Type the end-of-file indicator to terminate",
         "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
         "On Windows type <ctrl> z then press Enter");
         System.out.print("Enter a number: ");
      } // end while, should continue looping but doesn't
   } // end method main

   public static float minimum(ArrayList<Float> n){ // returns a float, takes an ArrayList with <Float> elements
      // assigns element at index 0 of n to min
      float min = n.get(0).floatValue();
      // iterates through i and assigns min to Math.min() called with previous
      // value of min and element at index i of n
      for(int i=1;i < n.size();i++){
         min = Math.min(min, n.get(i).floatValue());
      } // end for
      return min;
   } // end method minimum
} // end class FindMin

The problem is that the outer loop exits unexpectedly. It's my understanding that input.hasNext() prompts the user for input and returns true if there is input, if not, it returns false. It doesn't seem to check for input though. Can someone tell me what's happening?

NiaKitty
  • 93
  • 4

1 Answers1

0

One problem that you have going there that is likely doing what you think is the nested whiles.

Basically, once the inner while loop terminates, because it has the same condition as the outer while loop, the outer loop will execute the code between the inner loop and the ending brace and then exit as well. You said it yourself in the comments in the code- once the EOF is given, both loops terminate.

erzr2
  • 155
  • 3
  • 12