# The nth highest number from a list

• April 28th, 2012, 10:05 PM
sbjibo
The nth highest number from a list
Ok so this is a program where the user inputs a bunch of numbers and the program finds the nth highest number from the list, the nth number is specified by the user.

This program works like this: user enters numbers and all the numbers are added into a list. Then the user enter the "nth" highest number they want. then it enters a loop and executes the loop "nth" times. Each time, the highest number in the list is deleted using the .remove() method. after the loop, the program finds the highest number of what ever is left in the list and prints it.

Here is the code:
////////////////////////////////////////////////////////////////
Code java:

```import java.util.*;   public class nthHighest { public static void main(String[] args) { List<Double> numbers = new ArrayList<Double>(); Scanner input = new Scanner(System.in); System.out.println("How many numbers do you want to input"); int howMuch = input.nextInt(); // how many numbers the user want to input double num = 0; for (int a = 1; a <= howMuch; a++) { num = input.nextDouble(); numbers.add(num);//the numbers are added to the list } System.out.println("The nth highest number you want to find :"); int nth = input.nextInt();//the highest number user wants to find int se; double highest = numbers.get(0); double nthHighest = 0; int size = numbers.size(); se = 1; for (int h = 1; h <= nth; h++) { for (; se < size; se++) { if (numbers.get(se) > highest) { highest = numbers.get(se); } } numbers.remove(highest);//removes the highest number from the list size = numbers.size(); se = 0; } nthHighest = numbers.get(0); for (int q = 0; q < numbers.size();q++){//find the nth highest number if (numbers.get(q) > nthHighest ) nthHighest = numbers.get(q); System.out.print(" " + numbers.get(q)); } System.out.println(); System.out.println ("the " + nth + "th highest number is " + nthHighest); } }```
• April 29th, 2012, 02:09 AM
Parranoia
Re: The nth highest number from a list
What you could do instead, is sort the list, then grab the value from the list at position (numbers.size() - nth)
• April 29th, 2012, 07:46 AM
sbjibo
Re: The nth highest number from a list
I figured out the problem. After the highest number is deleted, you have to set 'highest' equal to the second highest or the lowest number. or else no number will be removed from the list.
• April 29th, 2012, 04:38 PM
hackthisred
Re: The nth highest number from a list
You will probably want to use a List so you can use sorting. This will give you a list of values from low to high, simply indicate the nth greatest value you wish to retrieve. It is simple and clean this way::-?

...removed by moderator
• April 29th, 2012, 06:36 PM
copeg
Re: The nth highest number from a list
hackthisred, welcome to the forums. Please read the forum rules, and I recommend reading as well the following:
http://www.javaprogrammingforums.com...n-feeding.html
• April 29th, 2012, 10:51 PM
hackthisred
Re: The nth highest number from a list
Thanks for the insight, but the member had already solved their question; I was merely showing another method of accomplishing the exact same task in cleaner syntax.
• April 30th, 2012, 11:20 AM
copeg
Re: The nth highest number from a list
Quote:

Originally Posted by hackthisred
Thanks for the insight, but the member had already solved their question; I was merely showing another method of accomplishing the exact same task in cleaner syntax.

I am not demoting your contribution above whatsoever. I am stating the rules of the forums, and if you read the link I would hope you would understand the rule, what we might consider spoon feeding, why it is bad, why your code was removed, and why we prefer to point folks in a certain direction rather than giving them the endpoint - the latter denies them the journey (which is one of the biggest tools to learn in programming - problem solving).