Reputation: 23
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]);
}
}
}
}
}
Upvotes: 0
Views: 205
Reputation: 31
You are not calculating max anywhere.
Replace:
int max = array[j]
With:
max = max > array[j] ? max : array[j];
To store the maximum value in the array. (This is a ternary operator)
Now the array have have equal values so in that case take care of this possibility by this code :
for(int i = 0; i <= 4; i++) {
if(array[i] == max) {
System.out.println("Max endangered:" + array[i] + name[i]);
}
}
Upvotes: 0
Reputation: 8229
What you can do is sort the array using a built-in method and then retrieve the last value (the greatest) and first value (the smallest).
Arrays.sort(array);
int max = array[array.length - 1];
int min = array[0];
For more on the sort
method in Java's Arrays
class, here's a description of what it exactly does,
public static void sort(int[] a)
Sorts the specified array into ascending numerical order.
Parameters: a - the array to be sorted
If you want to get the corresponding animal then I would suggest ignoring the above and use streams in Java 8. Combine the String and int array into one 2D String array. Make the rows equal to the number of animals and the columns equal to 2 (ex: for 5 animals, String[][] array = new String[5][2]
). For each row in the 2D array, the first element should be the animal, and the second element should be the size (ex: String [][] array = {{"fox", "1"},{"deer","0"},{"bear", "2"}}
). The following will return you a 2D array that is sorted in ascending order and gives you the corresponding animal with it,
String[][] sorted = Arrays.stream(array)
.sorted(Comparator.comparing(x -> Integer.parseInt(x[1])))
.toArray(String[][]::new);
String smallestAnimal = sorted[0][0]; //name of smallest animal
String smallest = sorted[0][1]; //population of smallest animal
String biggestAnimal = sorted[sorted.length - 1][0]; //name of biggest animal
String biggest = sorted[sorted.length - 1][1]; //population of biggest animal
Upvotes: 2
Reputation: 2132
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]);
}
Upvotes: 0