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.

1 Visitor Messages

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



    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);
    }
    }
Showing Visitor Messages 1 to 1 of 1
About sbjibo

Basic Information

About sbjibo
Java Skill Level:
Beginner

Statistics


Total Posts
Total Posts
18
Posts Per Day
0.00
Visitor Messages
Total Messages
1
Most Recent Message
April 28th, 2012 10:00 PM
Total Thanks
Total Thanks
2
  • Thanked 0 Times in 0 Posts
General Information
Last Activity
June 27th, 2012 09:10 PM
Join Date
April 28th, 2012