1

Hi so I'm new at java programming and i'm currently at learning array. So what i'm trying to do is find the maximum and minimum value of an array but for some reason i cant find the minimum value but i can find the maximum value.

Here is my output:

Enter the number of elements: 5                                                                                                               
Enter a number: 1                                                                                                                             
Enter a number: 2                                                                                                                             
Enter a number: 3                                                                                                                             
Enter a number: 4                                                                                                                             
Enter a number: 5                                                                                                                             
The maximum number is:5
The minimum number is: 0

I've used the same statement for getting the max value by only changing the operator. But somehow the output is always zero.

Here is my code:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {



        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of elements: ");
        int elements = input.nextInt();

        int [] array = new int[elements];
        int max = array[0];
        int min = array[0];

        for(int i = 0; i<elements; i++){

            System.out.print("Enter a number: ");
            array[i] = input.nextInt();

            if(array[i]>max){
                max = array[i];

            }
            if(array[i]<min){
                min = array[i];
            }

        }

        System.out.print("The maximum number is:" + max);
        System.out.println();
        System.out.print("The minimum number is: " + min);






    }
}

Any help would be appreciated thanks!

1
  • because you are not inputting negative numbers in your input. Your min is initialized by default to 0. Commented Dec 1, 2019 at 6:23

6 Answers 6

3
int min = array[0];

This statement is executed at a time when array[0] is zero.

You have not read any values at this point, so initialize your min and max values with:

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

In fact, you don't need the array at all, since you don't use it after the loop. Just assign the scanner result to an int variable, declared inside the loop.

Sign up to request clarification or add additional context in comments.

Comments

1

Assing highest value to min and lowest value to max

min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;

Comments

0

When you allocate memory using new operator,

int [] array = new int[elements];

each element of array is initialized to zero.

So when you do int min=array[0], min is assigned value 0. Then on comparing with 1,2,3,4 and 5 ,min is smallest and hence does not change.

To solve this, First input all the elements in array using for loop , then initialize min and max to array[0]

Comments

-1

Try this

import java.util.Scanner;

    public class Main
    {
        public static void main(String[] args) {



            Scanner input = new Scanner(System.in);
            System.out.print("Enter the number of elements: ");
            int elements = input.nextInt();

            int [] array = new int[elements];


            for(int i = 0; i<elements; i++){

                System.out.print("Enter a number: ");
                array[i] = input.nextInt();
             }
            int max = array[0];
            int min = array[0];
            for(int i = 0; i<elements; i++){
                if(array[i]>max){
                    max = array[i];

                }
                if(array[i]<min){
                    min = array[i];
                }


           }

            System.out.print("The maximum number is:" + max);
            System.out.println();
            System.out.print("The minimum number is: " + min);






        }
    }

Comments

-1

The default initial value of an Integer is 0. for this reason when checking if it is bigger than the input value the default value stays the same

2 Comments

The default value of an Integer is null. I suppose you mean int.
The OP's code contains an array of int and in java every element of such an array is implicitly initialised to 0 (zero). I think your answer is misleading.
-1

Why do you need an array for this in the first place?

Scanner console = new Scanner(System.in);
int entries = console.nextInt();
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i = 0; i < entries; i++){
   int temp = console.nextInt();
   if(temp < min) {
        min = temp;
   }
   if (temp > max) {
        max = temp;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.