Zain Mohamed Zain Mohamed - 1 month ago 27
Java Question

How would I determine the smallest value which has been entered?

My code asks the user to enter values of animal species and then displays it back to them. I just need to finally add a part which also tells the user the most endangered animal (the one will the lowest entered number). I've looked around on some places and triec using x< min or x=MAX_VALE etc. I just can't seem to make anything work. Is there a method which would be more appropriate for my program?

import java.util.Scanner;

public class endangered
{
public static void main(String[] param)
{
animal();
System.exit(0);
}

public static void animal()
{
int[] array = new int[5];
int j = 0;
String[] names = {"Komodo Dragon" , "Manatee" , "Kakapo" , "Florida Panther" , "White Rhino"};
System.out.println("Please enter the number of wild Komodo Dragons, Manatee, Kakapo, Florida Panthers and White Rhinos.");
Scanner scan = new Scanner(System.in);
for (int i=0; i<=4; i++)
{
while(scan.hasNextInt())
{
array[j] = scan.nextInt();
int max = array[j];
j++;
if(j==5)
{
System.out.println(array[0] + ", " + names[0]);
System.out.println(array[1] + ", " + names[1]);
System.out.println(array[2] + ", " + names[2]);
System.out.println(array[3] + ", " + names[3]);
System.out.println(array[4] + ", " + names[4]);
}
}
}
}
}

Answer

In java 8, you can do something like this:

int min = Arrays.stream(array).reduce(Integer.MAX_VALUE, Math::min);

Update: The user asked for print of the animal as well.

If you need to return the animal as well, it would be best if we edit your code. We will add 2 more variables. The first one, minVal will contain the lowest number that the user entered. The second one, minValIndex will contain the index of the species with the lowest count. I removed your while cycle because there was no need for one.

  public static void animal()
    {
        int[] array = new int[5];
        int j = 0;
        String[] names = {"Komodo Dragon" , "Manatee" , "Kakapo" , "Florida Panther" , "White Rhino"};
        System.out.println("Please enter the number of wild Komodo Dragons, Manatee, Kakapo, Florida Panthers and White Rhinos.");
        Scanner scan = new Scanner(System.in);
        int minVal = Integer.MAX_VALUE;
        int minValIndex = -1;
        for (int i=0; i<=4; i++)
        {
                array[j] = scan.nextInt();
                if(array[j] < minVal) {
                    minVal = array[j];
                    minValIndex = j;
                }
                int max = array[j];
                j++;
                if(j==5)
                {
                    System.out.println(array[0] + ", " + names[0]);
                    System.out.println(array[1] + ", " + names[1]);
                    System.out.println(array[2] + ", " + names[2]);
                    System.out.println(array[3] + ", " + names[3]);
                    System.out.println(array[4] + ", " + names[4]);
                }

        }
        System.out.println("The smallest entered number:" + minVal);
        System.out.println("The species:" + names[minValIndex]);
    }
Comments