I want to be able to display the output as three lines of text at the end but I'm getting a really weird output. So for example
"The average is z"
"Min Value: x"
"Max Value: y"
But I am getting this:
Please enter the size of the array:
3
Please enter array elements:
10
The average is 3.
Min Value: 0
Please enter array elements:
20
The average is 10.
Max Value: 20
Min Value: 0
Please enter array elements:
30
The average is 20.
Max Value: 20
Max Value: 30
package arrays;
import java.util.Scanner;
public class AssignmentArrays {
public static void main (String[] args) {
Scanner inputs = new Scanner(System.in);
System.out.println("Please enter the size of the array: ");
int size = inputs.nextInt();
int[] Array = new int[size];
for (int i = 0; i < size;) {
System.out.println("Please enter array elements: ");
int value = inputs.nextInt();
if (value >=100 || value <=0) {
System.out.println("Only values greater than 0 and less than 100 can be accepted.");
}
else {
Array[i] = value;
i++;
average(Array);
getMaxValue(Array);
getMinValue(Array);
}
}
}
public static int average(int[] Array)
{
int sum = 0;
int average;
for (int i = 0; i < Array.length; i++)
sum = sum+Array[i];
{
average=sum/Array.length;
System.out.println("The average is " +average +".");
return average;
}
}
public static int getMaxValue(int[] Array) {
int maxValue = Array[0];
for (int i = 1; i < Array.length; i++){
if (Array[i] > maxValue){
maxValue = Array[i];
System.out.println("Max Value: " + maxValue);
}
}
return maxValue;
}
public static int getMinValue(int[] Array) {
int minValue = Array[0];
for (int i = 1; i < Array.length; i++){
if (Array[i] < minValue) {
minValue = Array[i];
System.out.println("Min Value: " + minValue);
}
}
return minValue;
}
}
Thank you for all your help! I have finished my assignment.
example output: https://i.stack.imgur.com/Doy2d.png :)
package arrays;
import java.util.Scanner;
public class Array_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inputs = new Scanner(System.in);
System.out.println("Please enter the size of the array: ");
int size = inputs.nextInt();
int[] Array = new int[size];
for (int i = 0; i < size;) {
System.out.println("Please enter array elements: ");
int value = inputs.nextInt();
if (value >=100 || value <=0) {
System.out.println("Only values greater than 0 and less than 100 can be accepted.");
}
else {
Array[i] = value;
i++;
}
}
Average(Array);
int max;
max = Max(Array);
System.out.println("The maximum is " +max + ".");
int min;
min = Min(Array);
System.out.println("The minimum is " +min + ".");
}
public static void Average(int[] Array) {
int sum = 0;
int average;
for (int i = 0; i < Array.length; i++)
sum = sum+Array[i];
average=sum/Array.length;
System.out.println("The average is " +average +".");}
public static int Max(int[]Array) {
int max= Array[0];
for (int i = 1; i < Array.length; i++) {
if (Array[i] > max) {
max = Array[i];
}
}
return max;
}
public static int Min(int[]Array) {
int min = Array[0];
for (int i = 1; i < Array.length; i++) {
if (Array[i] < min) {
min = Array[i];
}
}
return min;
Related
import java.util.Arrays;
public class Swap
{
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
System.out.print("What is the size of your array? ");
int myArray = console.nextInt();
int[] size = new int[myArray];
int sum = 0;
int max = 0;
int min = 100;
int temp = 0;
for (int i = 0; i < size.length; i++)
{
System.out.print("Array index " + (i) + ": ");
size[i] = console.nextInt();
sum += size[i];
if (size[i] > max) max = size [i];
if (size[i] < min) min = size [i];
}
System.out.println ("\nmaximum value is: " + max);
System.out.println ("\nminimum value is: " + min);
System.out.println (Arrays.toString(size));
temp = size[max];
size[max] = size[min];
size[min] = temp;
System.out.println (Arrays.toString(size));
}
}
I am having trouble swapping the min and max value in the array, I am able to find those values fine, and I even found a way to swap in with the temp variable, but I can't translate that into the array.
You're using max and min as array index to swap your values.That is not correct.Instead you've to keep min and max indexes,and use them to swap in the array. I suggest you to complete your code like that:
import java.util.Arrays;
public class Swap
{
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
System.out.print("What is the size of your array? ");
int myArray = console.nextInt();
int[] size = new int[myArray];
int sum = 0;
int max = 0;
int min = 100;
int maxIndex=0;
int minIndex=0;
int temp = 0;
for (int i = 0; i < size.length; i++)
{
System.out.print("Array index " + (i) + ": ");
size[i] = console.nextInt();
sum += size[i];
if (size[i] > max){
max = size [i];
maxIndex=i;
}
if (size[i] < min) {
min = size [i];
minIndex=i;
}
}
System.out.println ("\nmaximum value is: " + max);
System.out.println ("\nminimum value is: " + min);
System.out.println (Arrays.toString(size));
temp = size[maxIndex];
size[maxIndex] = size[minIndex];
size[minIndex] = temp;
System.out.println (Arrays.toString(size));
}
}
Only the part for max and min.
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int iAtMax = -1;
int iAtMin = -1;
for (int i = 0; i < size.length; i++) {
if (iAtMax == -1 || max < size[i]) {
iAtMax = i;
max = size[i];
}
if (min <= size[i]) {
iAtMin = i;
min = size[i];
}
}
if (iAtMax != -1) {
int temp = size[iAtMax];
size[iAtMax] = size[iAtMin];
size[iAtMin] = temp;
}
You could initialize max and min so that an update in the loop always updates. Then you have to use <= max resp. >= min to update the indices. (See min above) or you can check whether there already is a max and min.
If the array is empty (or size 1) you cannot (resp. need not) swap.
I am attempting some array work for school. I am getting a user input for the size of the array, which will be filled with random elements. After I need to do some simple math; how many odd numbers, even numbers, sum of all etc. I have wrote everything, however the methods I wrote for the maths, does not compute to my main program, I am just getting "0"s for everything.
Excuse all of the comments, as you can probably tell, I have been trying multiple things.
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//int sum, even, odd, min, max, size;
//int [] intArray;
//double average;
//int even = 0, odd = 0, min = 0, max = 0, size = 0, sum = 0;
//double average;
System.out.print("Please enter a number for the array size (zero or greater): ");
int n=in.nextInt();
if (n<0){
System.out.println("ERROR: Number must be at least zero!!!");
}else {
IntArray mainArray = new IntArray(n);
}
//com.company.IntMath intMath = new com.company.IntMath(average, even, odd, min, max, size, sum);
IntArray.IntMath intMath = new IntArray.IntMath();
//intMath.average();
//intMath.even();
//intMath.getOdd();
//intMath.getMin();
//intMath.getMax();
//intMath.getSize();
//intMath.getSum();
System.out.println("Average: " + intMath.average()); //print the average of the elements
System.out.println("Even Count: " + intMath.even()); //prints the count of all even numbers
System.out.println("Odd Count: " + intMath.odd()); //prints the count of all odd numbers
System.out.println("Min: " + intMath.min()); //prints the min number
System.out.println("Max: " + intMath.max()); //prints the max number
System.out.println("Size: " + n); //prints the size of the array
System.out.println("Sum: " + intMath.sum()); //prints the sum of all elements
}
}
package com.company;
import java.util.Arrays;
import java.util.Random;
public class IntArray {
private int[] intArray;
public IntArray(int n) {
Random randArray = new Random();
int[] intArray = new int[n];
intArray[0] = 0; //why does this not make the element 0 a 0?????????
for (int i = 0; i < n; i++) {
intArray[i] = randArray.nextInt(50); //I was getting very big random numbers, so I set the max to 50
}
System.out.println("Set: " + Arrays.toString(intArray));
}
public static class IntMath {
double average;
int sum;
int even;
int odd;
int max;
int min;
int size;
int[] intArray;
int n;
//public IntMath() {//making a new constructor!!!!!!!!!!!!!!!!!!!!!!!
// this.average=average;
// this.sum=sum;
// this.even=even;
// this.odd=odd;
// this.max=max;
// this.min=min;
// this.size=size;
public double average () {
average = 0;
double total = 0;
for (int i = 0; i < n; i++) {
total = total + intArray[i];
}
return average;
}
// n should equal array length??????????
public int sum() {
//find the sum of all elements
sum = 0;
for (int i = 0; i < n; i++) {
sum += intArray[i];
}
return sum;
}
public int even() {
even = 0;
for (int i = 0; i < n; i++) {
if (intArray[i] % 2 == 0) {
even++;
}
}
return even;
}
public int odd() {
//odd = 0;
for (int i = 0; i < n; i++) {
if (intArray[i] % 2 != 0) {
this.odd++;
}
}
return odd;
}
public int max() {
max = intArray[0];
for (int i = 1; i < n; i++) {
if (intArray[i] > max) {
max = intArray[i];
}
}
return max;
}
public int min() {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < intArray.length; j++) {
if (intArray[i] > intArray[j]) {
min = intArray[i];
intArray[i] = intArray[j];
intArray[j] = min;
}
}
}
return min;
}
public int size() {
size = n;
return size;
}
}
}
Seems like you are struggling with certain concepts so I would suggest you to look at this code and compare it with what you wrote to determine where things went wrong.
Main.java
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
IntArray mainArray = null;
System.out.print("Please enter a number for the array size (zero or greater): ");
int size = in.nextInt();
if (size < 0) {
System.err.println("ERROR: Number must be at least zero!!!");
} else {
mainArray = new IntArray(size);
IntArray.IntMath intMath = new IntArray.IntMath(mainArray.getIntArray());
System.out.println("Average: " + intMath.average()); //print the average of the elements
System.out.println("Even Count: " + intMath.even()); //prints the count of all even numbers
System.out.println("Odd Count: " + intMath.odd()); //prints the count of all odd numbers
System.out.println("Min: " + intMath.min()); //prints the min number
System.out.println("Max: " + intMath.max()); //prints the max number
System.out.println("Size: " + intMath.size()); //prints the size of the array
System.out.println("Sum: " + intMath.sum()); //prints the sum of all elements
}
in.close();
}
}
IntArray.java
import java.util.Arrays;
import java.util.Random;
public class IntArray {
private int[] intArray;
public IntArray(int size) {
Random randArray = new Random();
intArray = new int[size];
intArray[0] = 0;
for (int i = 0; i < size; i++) {
intArray[i] = randArray.nextInt(50);
}
System.out.println("Outer Array: " + Arrays.toString(intArray));
}
public int[] getIntArray() {
return intArray;
}
public static class IntMath {
double average;
int sum;
int even;
int odd;
int max;
int min;
int size;
int[] intArray;
public IntMath(int[] intArray) {
this.intArray = intArray;
}
public double average() {
average = 0;
double total = 0;
for (int i = 0; i < intArray.length; i++) {
total += intArray[i];
}
average = total / intArray.length;
return average;
}
public int sum() {
sum = 0;
for (int i = 0; i < intArray.length; i++) {
sum += intArray[i];
}
return sum;
}
public int even() {
even = 0;
for (int i = 0; i < intArray.length; i++) {
if (intArray[i] % 2 == 0) {
even++;
}
}
return even;
}
public int odd() {
for (int i = 0; i < intArray.length; i++) {
if (intArray[i] % 2 != 0) {
this.odd++;
}
}
return odd;
}
public int max() {
max = intArray[0];
for (int i = 1; i < intArray.length; i++) {
if (intArray[i] > max) {
max = intArray[i];
}
}
return max;
}
public int min() {
min = intArray[0];
for(int i = 1; i < intArray.length; i++){
if(intArray[i] < min){
min = intArray[i];
}
}
return min;
}
public int size() {
size = intArray.length;
return size;
}
}
}
Output
Please enter a number for the array size (zero or greater): 5
Set: [0, 45, 10, 20, 45]
Average: 24.0
Even Count: 3
Odd Count: 2
Min: 0
Max: 45
Size: 5
Sum: 120
Do accept this answer if it solves your problem.
I want to get the minimum value and maximum value inside an array. After the user inputs n number of arrays, it will print the minimum and max value inside that array.
Here is an example output
Here is my code so far
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
System.out.println("Enter array size: ");
int n = input.nextInt();
int [] array = new int[n];
int max = getMaxValue(array);
int min = getMinValue(array);
System.out.println("Enter " + n + " elements:");
for (int i = 1; i <= array.length; i++) {
array[i] = input.nextInt();
}
System.out.println("Max Value: " + max);
System.out.println("Min Value: " + min);
} catch (InputMismatchException e) {
System.out.println("INVALID INPUT >> PLEASE INPUT A NUMBER");
}
}
private static int getMaxValue(int[] array) {
int maxValue = array[0];
for (int i = 1; i < array.length; i++){
if (array[i] > maxValue){
maxValue = array[i];
}
}
return maxValue;
}
private static int getMinValue(int[] array) {
int minValue = array[0];
for (int i = 1; i < array.length; i++){
if (array[i] < minValue) {
minValue = array[i];
}
}
return minValue;
}
}
There are a few problems keeping this program from working:
getMinValue and getMaxValue should be called after your input loop
All 3 of your loops index starting from 1, they should start from 0
The input loop repeats until i <= array.length, but it should end at i < array.length
Problem 1 is that these method are called on the empty array.
Problems 2 and 3 will cause an ArrayIndexOutOfBoundsException.
Here's how the corrections would look in your code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
System.out.println("Enter array size: ");
int n = input.nextInt();
int [] array = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < array.length; i++) {
array[i] = input.nextInt();
}
int max = getMaxValue(array);
int min = getMinValue(array);
System.out.println("Max Value: " + max);
System.out.println("Min Value: " + min);
} catch (InputMismatchException e) {
System.out.println("INVALID INPUT >> PLEASE INPUT A NUMBER");
}
}
private static int getMaxValue(int[] array) {
int maxValue = array[0];
for (int i = 0; i < array.length; i++){
if (array[i] > maxValue){
maxValue = array[i];
}
}
return maxValue;
}
private static int getMinValue(int[] array) {
int minValue = array[0];
for (int i = 0; i < array.length; i++){
if (array[i] < minValue) {
minValue = array[i];
}
}
return minValue;
}
}
There were 3 problems in your code which are as follows:
int max = getMaxValue(array) int min = getMinValue(array) are called in wrong places
when we initialize an int array the default value present in it is0.
So when you had call getMaxValue().
array[0] = 0 is compared with all rest of the values in the array (which are 0) and from all 0 is the maximum.
Same goes for getMinValue().
You have called the function getMaxValue() and getMinValue() without filling the array,with user input.
you should have used a for loop from 0 to array.length.
for(int index = 0 ; index < array.length ; index++){
array[index] = input.nextInt();
}
Has you would have noticed, I have index staring from 0 to array size.
what you did:
for (int i = 1; i <= array.length; i++) {
array[i] = input.nextInt();
}
this will cause ArrayIndexOutOfBoundException Because array indexing starts from 0 to n-1 and you allowed it to run till 1 to n.
Additionally, the finding of min and max can be done in one function.
by keeping the return type as an array.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
System.out.println("Enter array size: ");
int n = input.nextInt();
int [] array = new int[n];
System.out.println("Enter " + n + " elements:"); // <-- changed
for (int i = 0; i < array.length; i++) {
array[i] = input.nextInt();
}
int max_min[] = getMax_MinValue(array); // <- notice this.
System.out.println("Max Value: " + max_min[0]);
System.out.println("Min Value: " + max_min[1]);
} catch (InputMismatchException e) {
System.out.println("INVALID INPUT >> PLEASE INPUT A NUMBER");
}finally {
input.close(); // close the scanner
}
}
// return type changed
private static int[] getMax_MinValue(int[] array) {
int maxValue = array[0];
int minValue = array[0];
for (int i = 0; i < array.length; i++){
if (array[i] > maxValue){
maxValue = array[i];
}
if(array[i] < minValue) {
minValue = array[i];
}
}
int min_max[] = {maxValue,minValue}; // min_max[0] has max value and min_max[1] has min value
return min_max;
}
Output:
Enter array size:
8
Enter 8 elements:
5
23
9
14
45
36
42
39
Max Value: 45
Min Value: 5
This is not an appropriate answer.
However as the implementations of getMaxValue & getMinValue have a slight flaw: empty arrays will raise an ArrayIndexOutOfBoundException.
And especially one might use the Stream classes.
private static int getMaxValue(int[] array) {
return IntStream.of(array).max().orElse(0);
}
private static int getMinValue(int[] array) {
return IntStream.of(array).min().orElse(0);
}
max and min yield an OptionalInt, and for empty arrays one may give a default (orElse).
Hi in my program I want to display the average number, largest number, lowest number, and the mode in the array. So far only my average works and my methods for min and max both = the first number I enter. For example if I say I want to enter 3 numbers and I put 12, 15,and 6. The min and max will both output 12 since it's the first number entered and that's wrong so please help. Here's my code.
int amount;
System.out.println(" Enter the amount of numbers you would like to enter: ");
amount = scan.nextInt();
int [] arr = new int [amount];
int outcome = 1;
for (int i = 0; i < arr.length; i++){
System.out.println("Enter a number 1 through 50");
outcome = scan.nextInt();
arr [i] = outcome;
}
System.out.println(" ");
System.out.println( " The average is" );
System.out.println(average(arr));
System.out.println(" ");
System.out.println( " The lowest value in the array is " );
System.out.println(min(arr));
System.out.println(" ");
System.out.println( " The largest value in the array is " );
System.out.println(max(arr));
System.out.println(" ");
}
public static double average ( int [] arr) {
double sum = 0;
int value = arr.length;
for ( int i = 0; i < arr.length; i++){
sum += arr [i];
}
sum = sum / value;
return sum;
}
public static int min (int [] arr) {
int shortest = 0;
int smallest = 100;
int length = arr.length;
for ( int i = 0; i < arr.length; i ++ ) {
if ( length < smallest)
shortest += arr[i];
smallest = arr.length;
}
return shortest;
}
public static int max (int [] arr) {
int largest = 0;
int biggest = 0;
int length = arr.length;
for ( int i = 0; i < arr.length; i ++ ) {
if ( length > largest)
biggest += arr[i];
largest = arr.length;
}
return biggest;
}
}
This would be pretty straightforward. The reason both your min and max methods return 12 is because you set both largest and smallest to arr.length and thus immediately stop the for-loop after only one run. Also, there are far easier implementations for this kind of problem. Try doing something along these lines:
public int getMax(int[] arr)
{
int max = arr[0]; //To have a baseline
for(int i = 1; i < arr.length; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
}
return max;
}
public int getMin(int[] arr)
{
int min = arr[0]; //To have a baseline
for(int i = 1; i < arr.length; i++)
{
if(arr[i] < min)
{
min = arr[i];
}
}
return min;
}
This is both far more readable and easier to execute. Just ask if you have any questions :-)
Can somebody help me what's wrong with my codes I'm having trouble with the displaying the largest and the smallest part in the array. Sorry I'm completely new with java. Thanks a bunch
package problem6;
import java.util.Scanner;
public class Problem6 {
public static void main(String[] args) {
int input;
int min = 0;
int max = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");
input = keyboard.nextInt();
int array[] = new int[input];
for (int i = 0 ; i < array.length; i++ ) {
System.out.println("Enter number: ");
array[i] = keyboard.nextInt();
}
{
if (input > max)
{
max = input;
}
else if (input <= min)
{
min = input;
}
}
System.out.print("\nLargest: " + max);
System.out.print("\nSmallest:" + min);
}
}
Post you read the value, you need to iterate over the array and try to use your logic of mix/max comparison like:
int min = Integer.MAX_VALUE;//change your assignment of 0 as numbers can be negative
int max = Integer.MIN_VALUE;
for (int number : array) {//use separate for loop or use the same for loop to which you add numbers in array.
if (number > max) {
max = input;
}
else if (number < min) {
min = input;
}
}
With using the same for loop:
int min = Integer.MAX_VALUE;//change your assignment of 0 as numbers can be negative
int max = Integer.MIN_VALUE;
for (int i = 0 ; i < array.length; i++ ) {
System.out.println("Enter number: ");
array[i] = keyboard.nextInt();
if (array[i] > max) {
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
The errors were
an extra pair of braces, and 2. comparing input-size instead of array elements with min and max.
The probable working code is shown below :-
package problem6;
import java.util.Scanner;
public class Problem6 {
public static void main(String[] args) {
int input;
int min = 0;
int max = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");
input = keyboard.nextInt();
int array[] = new int[input];
for (int i = 0 ; i < array.length; i++ ) {
System.out.println("Enter number: ");
array[i] = keyboard.nextInt();
if (array[i] >= max)
{
max = array[i];
}
else if (array[i] <= min)
{
min = array[i];
}
}
System.out.print("\nLargest: " + max);
System.out.print("\nSmallest:" + min);
}
}
You can even try this.
int max=array[0], min=array[0];
for(int x=0; x<array.length; x++){
max = array[x]>max?array[x]:max;
min = array[x]<min?array[x]:min;
}
Set the first array element as min and max first.
You need to have this code:
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0 ; i < array.length; i++ ) {
if (array[i]> max)
{
max = array[i];
}
else if (array[i] <= min)
{
min = array[i];
}
}
System.out.print("\nLargest: " + max);
System.out.print("\nSmallest:" + min);