Java Im having trouble with getting minimum values and print

[Java]

I\'m having trouble with getting minimum values and printing integer values only among the double values.

Please do not provide any methods like array, arraylist, buffer something like those things.

I\'ve only learned decision methods(if, else if), loops(while, for), and scanner. That is it.

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. 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 my code]

import java.util.Scanner;
public class Input
{

   public static void main(String[] args)
   {
       System.out.print(\"input: \");
       Scanner scan = new Scanner(System.in);
       int count = 0;
       double sum = 0;
       double average = 0;
       double minimum = 0;
       String output = \"\";
       String integer = \"\";
       boolean a = false;
       while(!a)
       {
           if(scan.hasNextDouble())
           {
               count++;
               System.out.print(\"input: \");
               double userInput = scan.nextDouble();
               minimum = 0;

               if(minimum < userInput)
               {
                   minimum = userInput;
               }
          
               sum = sum + userInput;
               average = sum / count;
          
               if(count == 1)
               {
                   output = output + userInput;
               }
               else
               {
                   output = output + \", \" + userInput;
               }
           }
           else
           {
               System.out.println(output);
               System.out.println(minimum);
               System.out.println(average);
               break;
           }
       }
      
   }

}

Solution

package myProject;

import java.util.Scanner;
//Class Input definition
public class Input
{
   //Main method
public static void main(String[] args)
{
System.out.print(\"input: \");
//Scanner class object created to accept data
Scanner scan = new Scanner(System.in);
//Initializes the variables
double userInput = 0.0;
int count = 0;
double sum = 0;
double average = 0;
double minimum = 0;
String output = \"\";
String integer = \"\";
boolean a = false;
//Loops till non double number is entered
while(!a)
{
   //Checks for the for the double data
if(scan.hasNextDouble())
{
System.out.print(\"input: \");
//Accepts user input
userInput = scan.nextDouble();
//Extracts the integral part from double number
double integral = userInput - (userInput - (int)userInput);
//Checks if it is integer value and positive
if(integral == userInput && userInput >= 0)
{
   //Counter is increased by 1
   count++;
   //If counter is one
if(count == 1)
{
   //No comma for one input
   output = output + userInput;
   //First value is the minimum
   minimum = userInput;
}//end of if
  
//Otherwise
else
{
   //Numbers separated by comma
   output = output + \", \" + userInput;
}//End of else
  
//Checks for the minimum
//If the current user input is less than the earlier minimum then update the minimum with curretn number
if(userInput < minimum)
{
minimum = userInput;
}//End of if
//Calculates sum
   sum = sum + userInput;
   //Calculates average
average = sum / count;
}//End of if for positive and integral
}//End of if for hasNextDouble();
  
//Otherwise display the output
else
{
   //If no valid number is inputed
   if(count == 0)
{
   System.out.println(\"No input\");
   break;
}//end of if
  
   //Otherwise valid numbers are available
System.out.println(\"The integers: \" + output);
System.out.println(\"The minimum number: \" + minimum);
System.out.println(\"The average of all the numbers: \" + average);
break;
}//End of else
}//End of while
}//End of main method
}//End of class

Output:

input: -1.0
input: 3.6
input: 1.0
input: 2.0
input: 5.6
input: q
The integers: 1.0, 2.0
The minimum number: 1.0
The average of all the numbers: 1.5

[Java] I\'m having trouble with getting minimum values and printing integer values only among the double values. Please do not provide any methods like array, a
[Java] I\'m having trouble with getting minimum values and printing integer values only among the double values. Please do not provide any methods like array, a
[Java] I\'m having trouble with getting minimum values and printing integer values only among the double values. Please do not provide any methods like array, a
[Java] I\'m having trouble with getting minimum values and printing integer values only among the double values. Please do not provide any methods like array, a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site