Question
[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
[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
Enter a double or Q to quit: 10.0 Enter a double or Q to quit: 8.7 Enter a double or Q to quit: 0 Enter a double or Q to quit: 5.2 Enter a double or Q to quit: -1 Enter a double or Q to quit: Q 10.0, 0.0, -1.0 -1.0 8.35
Thank you for your help.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started