Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

Explain the economic basis for international business.

Answered: 1 week ago

Question

=+1. How can you animate it?

Answered: 1 week ago

Question

13-4 What are alternative methods for building information systems?

Answered: 1 week ago