Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: The nth highest number from a list

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

    But for some reason, this program just finds the second highest number. Could anyone please help?

    Here is the code:
    ////////////////////////////////////////////////////////////////
    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);
    	}
    }
    Last edited by helloworld922; April 29th, 2012 at 12:30 AM. Reason: please use [code] tags


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default 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)

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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
    Last edited by copeg; April 29th, 2012 at 06:36 PM. Reason: formatting
    f34r th3 kut3 1z

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,261
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default 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:
    The Problem with Spoon-feeding

  6. #6
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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.
    f34r th3 kut3 1z

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,261
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: The nth highest number from a list

    Quote Originally Posted by hackthisred View Post
    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).

Similar Threads

  1. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  2. How to get the list of highest score
    By pogi in forum Java Theory & Questions
    Replies: 4
    Last Post: July 1st, 2010, 09:50 PM
  3. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM
  4. Averages, Highest & using a .csv file ! ? ! ?
    By thebigtimeGNAR in forum Java Theory & Questions
    Replies: 3
    Last Post: April 14th, 2010, 11:35 AM
  5. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM

Tags for this Thread