so in my program is where the user enters list of numbers one at a time, and when I would end the list numbers with the "end" statement which is set to -1, and once I do that I get my average, and maximum, and minimum, my problem is that when I do get the minimum output it would be -1 everytime, I'm having trouble to remove the -1 from the array, any ideas???
import java.util.Scanner; //import scanner to user scanner tool
public class Average { //creating public class
public static void main(String[]args) { //creating public static main
Scanner input = new Scanner(System.in); //creating scanner input to grab user input
System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
double[] numbers = new double[20]; //creating 20 count array
double sum = 0;
int count = 0;
double average;
int end = -1;
for(int i = 0; i<numbers.length; i++) {
System.out.print("Enter a number: ");
numbers[i] = input.nextDouble();
if(numbers[i]== end) {
break;
}
sum+= numbers[i];
count++;
}
// gets average from user input of numbers
average = sum/count;
System.out.println("Average is: " + average);
double max = maxim(numbers);
System.out.println("Max: " + max);
double min = minim(numbers);
System.out.println("Min: " + min);
}
//method for finding out maximum number from user input
public static double maxim(double[] array) {
double maxNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] > maxNum) {
maxNum = array[i];
}
}
return maxNum;
}
//method for finding out minimum number from user input
public static double minim(double[] array) {
double minNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] < minNum && array[i]!=-1) {
minNum = array[i];
}
}
return minNum;
}
}
You need to docouple part of code that responsible for reading user input from code that compute statistics. And for remove -1 from resulting array you need simple don't put this value to result. When user input random number first check if it is not -1, and after that put it in result. Something like that:
import java.util.Scanner;
public class Average {
public static void main(String[]args) { //creating public static main
double[] numbers = readInputNumbers();
System.out.println("Average is: " + average(numbers));
System.out.println("Max: " + max(numbers));
System.out.println("Min: " + min(numbers));
}
public double[] readInputNumbers() {
Scanner input = new Scanner(System.in); //creating scanner input to grab user input
System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
double[] numbers = new double[20];
final int endInput = -1;
for(int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number: ");
double nextNumber = input.nextDouble();
if(nextNumber == endInput) {
break;
} else {
numbers[i] = nextNumber;
}
}
return numbers;
}
public static double max(double[] array) {
double maxNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] > maxNum) {
maxNum = array[i];
}
}
return maxNum;
}
public static double min(double[] array) {
double minNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] < minNum && array[i]!=-1) {
minNum = array[i];
}
}
return minNum;
}
public static double average(double[] numbers) {
double sum = 0;
for(int i = 0; i < numbers.length; i++) {
sum = sum + numbers[i];
}
return sum / numbers.length;
}
}
In jdk since 8 version your could be simplify this task like this:
import java.util.Scanner;
import java.util.stream.*;
public class Average {
public static void main(String[]args) { //creating public static main
double[] numbers = readInputNumbers();
DoubleSummaryStatistics statistics = DoubleStream.of(numbers).summaryStatistics();
System.out.println("Average is: " + statistics.getAverage()));
System.out.println("Max: " + statistics.getMax());
System.out.println("Min: " + statistics.getMin());
}
public double[] readInputNumbers() {
Scanner input = new Scanner(System.in); //creating scanner input to grab user input
System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
double[] numbers = new double[20];
final int endInput = -1;
for(int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number: ");
double nextNumber = input.nextDouble();
if(nextNumber == endInput) {
break;
} else {
numbers[i] = nextNumber;
}
}
return numbers;
}
}
You could take the input into a temp variables and store it in the array only if it isn't the end flag (-1). E.g.:
for(int i = 0; i<numbers.length; i++) {
System.out.print("Enter a number: ");
double temp = input.nextDouble();
if(temp == end) {
break;
}
numbers[i] = temp;
sum += numbers[i];
count++;
}
Related
This question already has answers here:
reading from console with scanner in a loop for
(2 answers)
Closed 3 years ago.
This program is to take input from the user and to give output back showing how many numbers were above the average of the array and below it. I'm trying to put a condition on the loops to exit getting input.
import java.util.Scanner;
public class analyzeScores {
public static void count(int[] list) {
Scanner input = new Scanner(System.in);
for(int i = 0; i < list.length;i++) {
if(list[i] != 0)
list[i] = input.nextInt();
}
}
public static void sorts(int[] lists, int average) {
int high = 0;
int low = 0;
for(int i = 0; i < lists.length; i++) {
if(lists[i] >= average) {
high +=1;
}
else {
low += 1;
}
}
System.out.println("The number of higher then average scores is " + high);
System.out.println("The number of lower then average scores is " + low);
}
public static void main(String[] args) {
int[] list = new int[10];
System.out.println("Enter the scores: ");
count(list);
int total = 0;
for (int i = 0; i < list.length;i++) {
total += list[i];
}
total = total / list.length;
sorts(list, total);
}
}
I'm trying to figure out how to implement a way to input 0 to exit the loop in the count(int[] list) method. I tried to implement if(list[i] != 0) but messes the whole code up
you just have to add an else condition to your if statement in the loop,
This should work if the rest of your code works
import java.util.Scanner;
public class analyzeScores {
public static void count(int[] list) {
Scanner input = new Scanner(System.in);
for(int i = 0; i < list.length;i++) {
if(list[i] != 0){
list[i] = input.nextInt();
}else{
break;
}
}
}
public static void sorts(int[] lists, int average) {
int high = 0;
int low = 0;
for(int i = 0; i < lists.length; i++) {
if(lists[i] >= average) {
high +=1;
}
else {
low += 1;
}
}
System.out.println("The number of higher then average scores is " + high);
System.out.println("The number of lower then average scores is " + low);
}
public static void main(String[] args) {
int[] list = new int[10];
System.out.println("Enter the scores: ");
count(list);
int total = 0;
for (int i = 0; i < list.length;i++) {
total += list[i];
}
total = total / list.length;
sorts(list, total);
}
}
This program asks users to input numbers which populates an array list. If the user inputs a 0, then the program sums all of the previous numbers inputted and averages them. It's not working for me, so how could I make this work?
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class Calculation {
static Toolkit tools = new Toolkit();
public static void main(String [] args) {
double average = 0.0;
double sum = 0;
int i =0;
int num = 0;
int nElements = 0;
Scanner console = new Scanner(System.in);
ArrayList<Integer> value = new ArrayList<Integer>();
System.out.println("Enter value: ");
num = console.nextInt();
while(num != 0) {
value.add(new Integer(num));
}
for(i = 0; i < value.size(); i++) {
sum += value.size();
}
average = sum / value.size();
System.out.println("Number of values read: " + value.size());
System.out.println("Values sum: " + sum + "Values average: " + average);
System.exit(0);
}
}
You need to read inside the loop, here you have:
public static void main(String [] args) {
double average = 0.0;
double sum = 0;
int i =0;
int num = 0;
int nElements = 0;
Scanner console = new Scanner(System.in);
ArrayList<Integer> value = new ArrayList<Integer>();
System.out.println("Enter value: ");
num = console.nextInt();
while(num != 0) {
value.add(new Integer(num));
num = console.nextInt();
}
for(i = 0; i < value.size(); i++) {
sum += value.get(i);
}
average = sum / value.size();
System.out.println("Number of values read: " + value.size());
System.out.println("Values sum: " + sum + " Values average: " + average);
System.exit(0);
}
so how would you find the largest and smallest number here?
import java.util.Scanner;
public class Loops2 {
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
for (int i = 0; i < 10; i++) {
int number = input.nextInt();
System.out.println(number);
}
}
}
Assuming that what you are trying to do is find the largest and smallest integers are in an array of integers:
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
//I will create the array here...
int[] nums = new int[10];
//assigning numbers/ints
for(int i = 0; i < 10; i++) {
nums[i] = input.nextInt();
}
//now to find the largest and smallest (in this order)
int largest = 0;
for(int j = 0; j < nums.length; j++)//usage of the 1-line rule :)
if(nums[j] > largest)
largest = nums[j];
int smallest = largest;
//I'm doing this, so that it keeps checking for something lower than the largest number...
for(int k = 0; k < nums.length; k++)//usage of the 1-line rule again :)
if(nums[k] < smallest)
smallest = nums[k];
System.out.println("Largest: " + largest);
System.out.println("Smallest: " + smallest);
}
Hope this helps!
This is probably the best way to implement it
import java.util.Scanner;
public class FindLargestSmallestNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
//array of 10 numbers
int numbers[] = new int[10];
for (int i = 0; i < 10; i++) {
int number = input.nextInt();
System.out.println(number);
numbers[i] = number ;
}
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
import java.util.Scanner;
public class LargestSmallestNumbers {
private static Scanner input;
public static void main(String[] args) {
int count,items;
int newnum =0 ;
int highest=0;
int lowest =0;
input = new Scanner(System.in);
System.out.println("How many numbers you want to enter?");
items = input.nextInt();
System.out.println("Enter "+items+" numbers: ");
for (count=0; count<items; count++){
newnum = input.nextInt();
if (highest<newnum)
highest=newnum;
if (lowest==0)
lowest=newnum;
else if (newnum<=lowest)
lowest=newnum;
}
System.out.println("The highest number is "+highest);
System.out.println("The lowest number is "+lowest);
}
}
List<Integer> list = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
for (int i = 0; i < 10; i++) {
int number = input.nextInt();
System.out.println(number);
list.add(number);
}
Collections.sort(list);
System.out.println("the small: "+ list.get(0));
System.out.println("the big: "+list.get(list.size() - 1));
Ive already wrote code that will take the temp for each month and then show and calculate the total, average, most and least rainfall for the year and output that. How do I replace the most and least rainfall with actually months names?
code so far:
import java.util.Scanner;
import java.text.DecimalFormat;
public class Rainfall
{
public static void main(String[] args)
{
String [] months={"Janurary","Febuary","March","April","May","June","July","August","September","October","November","December"};
final int MONTHS = 12;
double[] rain = new double[MONTHS];
initRain(rain);
double total = totalRain(rain);
double average = averageRain(rain, total);
int most = mostRain(rain);
int least = leastRain(rain);
// Decimal Format
DecimalFormat digit = new DecimalFormat("#.0");
// Output
System.out.println("The total rainfall of the year is " + digit.format(total));
System.out.println("The average rainfall of the year is " + digit.format(average));
System.out.println("The month with the highest amount of rain is " + (most + 1));
System.out.println("The month with the lowest amount of rain is " + (least + 1));
}
public static void initRain(double[] array)
{
Scanner keyboard = new Scanner(System.in);
for (int x = 0; x < array.length; x++)
{
System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
array[x] = keyboard.nextDouble();
}
}
public static double totalRain(double[] array)
{
double total = 0;
for (int x = 0; x < 12; x++)
total += array[x];
return total;
}
public static double averageRain(double[] array, double total)
{
return total / array.length;
}
public static int mostRain(double[] array)
{
double maximum = array[1];
int value = 0;
for (int i=0; i < 12; i++) {
if (array[i] >= maximum) {
maximum = array[i];
value = i;
}
}
return months[index];
}
public static int leastRain(double[] array)
{
double minimum = array[0];
int value = 0;
for (int i=0; i < 12; i++)
{
if (array[i] <= minimum) {
minimum = array[i];
value = i;
}
}
return value;
}
}
Well, you've got an index, and you've got an array of the names of the months, so... ?
String most = months[mostRain(rain)];
I guess that this code is not even compiling
see
public static int mostRain(double[] array)
{
double maximum = array[1];
int value = 0;
for (int i=0; i < 12; i++) {
if (array[i] >= maximum) {
maximum = array[i];
value = i;
}
}
return months[index];
}
as it is expecting to be returned an int but you are returning a String (also index does not exist), so what you want is
public static int mostRain(double[] array)
{
double maximum = 0.00; // change this too
int value = 0;
for (int i=0; i < 12; i++) {
if (array[i] >= maximum) {
maximum = array[i];
value = i;
}
}
return value; // change this
}
then you can use it above like
System.out.println("The month with the highest amount of rain is " + months [mostRain (rain)]);
Of course, make the similar change for leastRain too.
I'm stuck... My code was working earlier, but now it just hangs. On top of that I can't seem to get my getHighest and getSmallest methods to return the correct values. I don't know if I'm just not catching something. Any help would be great!
import java.util.Scanner;
import java.text.DecimalFormat;
public class Rainfall
{
public static void main(String[] args)
{
final int MONTHS = 12;
double[] rain = new double[MONTHS];
initRain(rain);
double total = totalRain(rain);
double average = averageRain(rain, total);
double most = mostRain(rain);
double least = leastRain(rain);
DecimalFormat digit = new DecimalFormat("#.0");
System.out.println("The total rainfall of the year is " + digit.format(total));
System.out.println("The average rainfall of the year is " + digit.format(average));
System.out.println("The month with the highest amount of rain is " + most);
System.out.println("The month with the lowest amount of rain is " + least);
}
public static void initRain(double[] array)
{
Scanner keyboard = new Scanner(System.in);
for (int x = 0; x < array.length; x++)
{
System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
array[x] = keyboard.nextDouble();
}
}
public static double totalRain(double[] array)
{
double total = 0;
for (int x = 0; x < 12; x++)
total += array[x];
return total;
}
public static double averageRain(double[] array, double total)
{
return total / array.length;
}
public static double mostRain(double[] array)
{;
double maximum = array[1];
int value = 0;
for (int i=0; i < 12; i = i++)
{
if (array[i] >= maximum)
maximum = array[i];
value = i;
}
return value;
}
public static double leastRain(double[] array)
{
double minimum = array[0];
int value;
for (int i=0; i < 12; i++)
{
if (array[i] <= minimum)
minimum = array[i];
value = i;
}
return value;
}
}
Your program hangs because of this line:
for (int i=0; i < 12; i = i++)
The problem is that i++ returns the value of i before the variable is incremented, so the increment step of your loop is the same as writing i=i. Thus, the variable i never reaches the escape condition of the loop.
it should be:
for (int i=0; i < 12; i++)
Cleaned up your code a little bit, can be improved a lot. There were many simple errors.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Rainfall
{
public static void main(String[] args)
{
final int MONTHS = 12;
double[] rain = new double[MONTHS];
initRain(rain);
double total = totalRain(rain);
double average = averageRain(rain, total);
int most = mostRain(rain);
int least = leastRain(rain);
DecimalFormat digit = new DecimalFormat("#.0");
System.out.println("The total rainfall of the year is " + digit.format(total));
System.out.println("The average rainfall of the year is " + digit.format(average));
System.out.println("The month with the highest amount of rain is " + (most + 1));
System.out.println("The month with the lowest amount of rain is " + (least + 1));
}
public static void initRain(double[] array)
{
Scanner keyboard = new Scanner(System.in);
for (int x = 0; x < array.length; x++)
{
System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
array[x] = keyboard.nextDouble();
}
}
public static double totalRain(double[] array)
{
double total = 0;
for (int x = 0; x < 12; x++)
total += array[x];
return total;
}
public static double averageRain(double[] array, double total)
{
return total / array.length;
}
public static int mostRain(double[] array)
{
double maximum = array[1];
int value = 0;
for (int i=0; i < 12; i++) {
if (array[i] >= maximum) {
maximum = array[i];
value = i;
}
}
return value;
}
public static int leastRain(double[] array)
{
double minimum = array[0];
int value = 0;
for (int i=0; i < 12; i++)
{
if (array[i] <= minimum) {
minimum = array[i];
value = i;
}
}
return value;
}
}