Find the two largest integers in a list I wanted to know if there was any way to improve on this solution. I believe it uses the least amount of variables. Also wanted to know how are my variable names? Are they sufficient? Do I need anymore indenting? I'm looking for perfection in a way. At least a clear, correct and readable program.

/*
 * File: FindTwoLargest.java
 * ----------------------
 * This program finds the two largest integers in a list.
 * 
 */

/* Library packages */
import acm.program.*;

public class FindTwoLargest extends ConsoleProgram {
    public void run() {
        println("This program finds the two largest integers in a list.");
        println("Enter values, one per line, using a 0 to signal the end of list.");
        int n = readInt("? ");
        int largest = n;
        int secLargest = n;
        while (true) {
            n = readInt("? ");
            if (n == SENTINEL) break;
            secLargest = n;
            while (true) {
                n = readInt("? ");
                if (n == SENTINEL) break;
                if (n &gt; largest) {
                    secLargest = largest;
                    largest = n;
                } else if (n &lt; largest &amp;&amp; n &gt; secLargest) {
                    secLargest = n;
                }
            }
            break;
        }
        println("The largest value is " + largest);
        println("The second largest value is " + secLargest);
    }

    /* Private constant */
    private static final int SENTINEL = 0;
}
Please bare in mind that this is my first review on here :)

I see that you wanted to do the whole sorting yourself and personally I think that's good to understand some basics. However in aspect of perforfmance and readability it's not quite the best. 
Why re-invent the wheel when you can use already existing solutions?

 /*
 * File: FindTwoLargest.java
 * ----------------------
 * This program finds the two largest integers in a list.
 * 
 */

/* Library packages */
import acm.program.*;

public class FindTwoLargest extends ConsoleProgram {

    /* Private constant */
    private static final int SENTINEL = 0;

    public void run() {
        println("This program finds the two largest integers in a list.");
        println("Enter values, one per line, using a 0 to signal the end of list.");
        List&lt;Integer&gt; userInput = new ArrayList&lt;&gt;();
        while (true) {
            int n = readInt("? ");
            if (n == SENTINEL) 
            {
              break;
            }
            userInput.add(n);
        }
        if(!userInput.isEmpty()) {
          java.util.Collections.sort(userInput);
          java.util.Collections.reverse(userInput);
          println("The largest value is " + userInput.get(0));
          println("The second largest value is " + userInput.get(1));
       }
    }
}


So that's a solution I would use. It only uses one while loop which is enough to work and uses less If-Else logic. You can just collect every input given to the list and then use the Collections API from Java to easily sort the list. The call to reverse will change the sorting from ascending to descending meaning the hightest and second highest values are at the beginning. 

However if that is not what you wanted or needed I would ask why you used two while loops? It's not necessary for the logic. 
You can check everything in the first while and remove duplicate code and unneeded checks, which reduce the readability and maintenance of the code.

Another hint would be that with else if you indicate that the previous condition was checked and didn't comply, meaning no need to check again what you did in the first if. You would only need to check that again if you just used a second if.

Which also improves some maintenance and readability, is moving constants and global variables before the constructor of a class. In case you didn't have contact with that yet, a Constructor is the first method in a class ( used for defining the initialization and so on but that doesn't belong here ) and comes before all custom methods. Meaning it's at the top of a class. And to easily get a grip of what is global or where the constants are you should always move those at the top of the class.

import acm.program.*;

public class FindTwoLargest extends ConsoleProgram {

    /* Private constant */
    private static final int SENTINEL = 0;

    public void run() {
        println("This program finds the two largest integers in a list.");
        println("Enter values, one per line, using a 0 to signal the end of list.");
        int n = readInt("? ");
        if (n == SENTINEL) {
            break;
        }
        int largest = n;
        int secLargest = n;
        while (true) {
            n = readInt("? ");
            if (n == SENTINEL) { // I just personally don't like inline if's
                break;
            }
            if ( n &gt;= largest ) {
                secLargest = largest;
                largest = n;
            } else if ( n &gt; secLargest ) {
                secLargest = n;
            }
        }
        println("The largest value is " + largest);
        println("The second largest value is " + secLargest);
    }
}


Note: I tested a slightly different variant of this in the ideone online compiler here.

So with the answer of Roland in mind here's what you can also do to fix those little bugs:


  1 5 5 0 (only one of the 5s is accepted, the other is ignored)


Just change the first if too if( n &gt;= largest ) if you don't want to ignore the second entry.


  5 0 (there is no second number at all, nevertheless your program says
  that the second-largest number is 5)


Again this depends on what you want to do. If you don't want to display a secondlargest in this case you could do something like this:

int secondLargest = 0;

...
if(secondLargest != 0){
  println("The second largest value is " + secLargest);
}



  0 (basically works, but the output should indicate that there wasn't a
  number at all)


I just did the sentinel check for the first input already. That allows the user to quit the program directly. If that's not what you want to do you can think about something else.