0

What I'm trying to do is get a set of numbers from the user, the console will print out the second highest number in english and the second lowest number in spanish.

First I'm trying to get the input from user, put it in an array and grab the second highest and second lowest value of the components. But I can't figure out how to grab ones I need.

public static void main(String[] args) {

    //get the length from user
    int length;
    Scanner input = new Scanner(System.in);

    System.out.println("How many variables are you going to enter?: ");

    length = input.nextInt();

    //allocate array for that length

    int[] variables = new int[length];

    for(int counter = 0; counter < length; counter++) {
        System.out.println("Enter variable: ");
        variables[counter] = input.nextInt();


    }

    input.close();

    //print the variables
    System.out.println("Your variables are");
    for(int counter = 0; counter < length; counter++) {
        System.out.println(variables[counter]);
    }


}
  • Possible duplicate of [Finding the max/min value in an array of primitives using Java](https://stackoverflow.com/questions/1484347/finding-the-max-min-value-in-an-array-of-primitives-using-java) – Ben Feb 20 '18 at 12:39
  • Side note: you're closing System.in now. – Neijwiert Feb 20 '18 at 12:40
  • You need to sort your array and then grab sortedArray[1] and sortedArray[length-1] elements from it. – Mindaugas Nakrošis Feb 20 '18 at 12:41
  • @MindaugasNakrošis sorting after is a more expensive operation than doing it before hand. He should determine the min/max or sort it while acquiring the numbers. – Neijwiert Feb 20 '18 at 12:42

2 Answers2

1
    System.out.println("------------------");
    Arrays.sort(variables);    //sort array
    System.out.println(variables[1]);   //2nd lowest value
    System.out.println(variables[variables.length-2]);  //2nd highest value 

try this brother

add this to your code

Rahul
  • 31
  • 5
0
class Demo
{
    public static void findNum(int arr[]){
        int largest = arr[0], second_largest = arr[0];
        int minimum = are[0], second_minimum = are[0];
        for(int i=0;i<arr.length;i++){
            if(arr[i] >= largest){
                second_largest = largest;
                largest = arr[i];
            }
            else if(arr[i]>second_largest){
                second_largest = arr[i];
            }
            if(arr[i] <= minimum){
                second_minimum = minimum;
                minimum = arr[i];
            }
            else if(arr[i]<second_minimum){
                second_minimum = arr[i];
            }

        }
        System.out.println("Second Largest = " + second_largest + " Second_minimum = "+second_minimum);
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        int arr[] = {5,3,2,1,8,77};
        findNum(arr);
    }
}

You can do something like this if you're particular about not sorting your original array.

Ashutosh
  • 529
  • 5
  • 15
  • Technically, when there's only 1 or 2 items in the array, nothing can be second largest or smallest. Or if all the items are the same? OP didn't really specify what the outcome should be. I also think there should at least be a mention about determining this beforehand instead of after getting all the numbers. – Neijwiert Feb 20 '18 at 12:56