Java Keep getting into an infinite loop So I have two inquir

[Java] Keep getting into an infinite loop.

So I have two inquiries.

First, why I\'m keep getting into an infinite loop.

Second, why I can\'t get the right output. (You\'ll have the expected output below)

Here\'s the problem:

Write an application called InputProcessing. In the main method you will use a loop to read a collection of doubles. Stop reading when the input can not be converted to a double. Use this exact prompt: System.out.print(\"Enter a double or Q to quit: \"); Copy and paste it. That is what I did when I coded the model solution.

Note: you will actually quit on any string that can not be converted to a double.

Do the following things:

Print a string of the inputs that have an integer value (like 4.0) You can use casting for this. (Ignoring casting part please. I didn\'t do that part yet) If none are integers, do not print anything. You can assume that none of the inputs is larger than the largest int or smaller than the smallest. Print them on one line separated by a \", \" (a comma and then a space.). You can do this by put the separator before every number except the first.

Find and print the minimum number in the input stream

Find and print the average of all the positive numbers or 0 if there are no positive numbers

You will only read the inputs one time and do all the processing as you go. No arrays yet.

The outputs should be on separate lines and in this order

the integers (or nothing)
the minimum number
the average of all the numbers or 0 if there are no positive numbers

If there were no inputs at all, just print \"no input\" and nothing else

Hint: To find the minimum number, . You can should initialize a variable to the first input. Use a boolean flag first initializes to true. If first is true (this is the first time through the loop), initialize the minimum to the first input and set first to false. After that when you read a number, you can use the find minimum algorithm.

Here\'s what I\'ve done:

import java.util.Scanner;
public class InputProcessing
{

   public static void main(String[] args)
   {
       System.out.print(\"Enter a double or Q to quit: \");
       Scanner input = new Scanner(System.in);
       double firstInput = input.nextDouble();
       double userInput = 0;
       double minimum = firstInput;
       boolean first = true;
       double average = 0;
       int i = 0;
      
       while(input.hasNextDouble())
       {
           if(first)
           {
               System.out.println(firstInput);
               first = false;
           }
          
           i++;
           System.out.print(\"Enter a double or Q to quit: \");
           userInput = input.nextDouble();
          
           while(userInput <= minimum)
           {
               minimum = userInput;
           }
           if(firstInput > 0 && userInput > 0)
           {
               average = 0;
           }
           else
           {
               average = (average + userInput) / i;
           }
          
       }
       System.out.println(firstInput + \", \" + userInput);
       System.out.println(average);
       System.out.println(minimum);
   }

}

[Expected Output]

Enter a double or Q to quit: 9.5

Thank you for your help.

Solution

I have updated the code for you. Your code had some logical loopholes, I commented them and i used BufferedReader since it is more flexible in reading lines.

public class InputProcessing
{

    public static void main(String[] args) throws IOException
    {
        System.out.print(\"Enter a double or Q to quit: \");
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        double firstInput; //this variable is not used and u can delete it
        double userInput;
        double minimum = Double.POSITIVE_INFINITY;
        boolean first; // this is also not used anymore
        double average = 0;
        int i = 0;
        String line = input.readLine();
        String output = null;
        while(!line.equals(\"Q\"))
        {
            /* we do not need to print here
            if(first)
            {
                System.out.println(firstInput);
                first = false;
            }
            */
            if (isNumeric(line)){
                userInput = Double.parseDouble(line);
                if(output == null)
                    output = \"\"+userInput;
                else
                    output += \", \"+userInput;
                if (userInput>0) {
                    average = (average*i + userInput)/(i+1);
                    i++;
                }
                while(userInput < minimum) // I dont know why you used a loop instead of if. And using <= operator you get infinite loop
                {
                    minimum = userInput;
                }
            }
            System.out.print(\"Enter a double or Q to quit: \");
            line = input.readLine();
        }
        System.out.println(output);
        System.out.println(average);
        System.out.println(minimum);
    }

    private static boolean isNumeric(String line) { //you should know how try catch works to understand this
        try {
            Double.parseDouble(line);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

}

[Java] Keep getting into an infinite loop. So I have two inquiries. First, why I\'m keep getting into an infinite loop. Second, why I can\'t get the right outpu
[Java] Keep getting into an infinite loop. So I have two inquiries. First, why I\'m keep getting into an infinite loop. Second, why I can\'t get the right outpu
[Java] Keep getting into an infinite loop. So I have two inquiries. First, why I\'m keep getting into an infinite loop. Second, why I can\'t get the right outpu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site