find the highest and lowest number in array with JOptionPane

Mikami

the code it's for education in college I need the user to insert a values of array and find which number is the highest and lowest but the lowest number doesn't work . can anyone explain this to me , sorry for asking much

int m=Integer.parseInt(JOptionPane.showInputDialog("Enter index"));       
int values;
int num [] = new int [m];

int max = num[0];
int min = num[0];
for (int i=0;i<num.length;i++)      
     {                          
     num[i] = values =Integer.parseInt(JOptionPane.showInputDialog("Enter value"));   
     System.out.println(num[i]);

     if (num[i] > max)
     {
         max=num[i];
     }
     if (num[i] < min )
     {
         min=num[i];
     }

}                                    
System.out.println("Largest Number in a given array is : " + max);
System.out.println("Smallest Number in a given array is : " + min);
sticky_elbows

You can use Arrays.sort() to sort the array and simply access the first(0th) index which will hold the smallest number, and the last index which will hold the largest number:

int m=Integer.parseInt(JOptionPane.showInputDialog("Enter index"));       
 //int values; dont need this
 int num [] = new int [m]; //num is initialised with length entered by the user

 //int max = num[0]; no need
 //int min = num[0]; no need
 for (int i=0;i<num.length;i++)      
 {                          
  num[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter value"));   
  System.out.println(num[i]);      

 }
Arrays.sort(num); //this will sort your array in ascending order: smallest number will be at the 0th index and largest will be at the last index                                    
System.out.println("Largest Number in a given array is : " + num[num.length-1]);
System.out.println("Smallest Number in a given array is : " + num[0]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Finding lowest and highest number number value in an Array

How to find the highest and lowest number with JS?

Find highest and lowest number from the string of numbers

Finding the lowest/highest number in an array using a loop

How to find the lowest number in an array?

What is the lowest and highest number of

FIND THE HIGHEST AND LOWEST SALARY

problems with highest and lowest in an array

How to I find lowest and highest value of this array (java)

How can I find highest and lowest in an array of objects/ Javascript

Find the lowest unused number in an array using Javascript

How to get the highest and lowest number

Find time of highest and lowest candles

Find the lowest and highest NUMBERS in a collection

int[] array (sort lowest to highest)

sort this array from highest to lowest

How to display highest and lowest of an array

Sort an array as [1st highest number, 1st lowest number, 2nd highest number, 2nd lowest number...]

Trouble with function that is supposed to find highest number in an array

Find Object in an array that has the highest number of keys

Java Comparator Sorting from Highest Number to Lowest

Identify the highest and lowest index of a pair of number

Displaying highest and lowest number values in C

Find the highest and lowest value for a time frame

Find the lowest / highest key of a distributed map

How to find the highest and lowest product price

LINQ - find between lowest and highest string

How to find highest and lowest value on substrings?

Find Nth highest/lowest values in Pine