fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.List;
  7. import java.util.Collections;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone
  14. {
  15. /* Private constant */
  16. private static final int SENTINEL = 0;
  17.  
  18. public static void main (String[] args) throws java.lang.Exception
  19. {
  20. System.out.println("This program finds the two largest integers in a list.");
  21. System.out.println("Enter values, one per line, using a 0 to signal the end of list.");
  22. String s = br.readLine();
  23. int n = Integer.valueOf(s);
  24. int largest = n;
  25. int secLargest = n;
  26. while (true) {
  27. s = br.readLine();
  28. n = Integer.valueOf(s);
  29. if (n == SENTINEL) { // I just personally don't like inline if's
  30. break;
  31. }
  32. if (n >= largest) {
  33. secLargest = largest;
  34. largest = n;
  35. } else if (n < largest && n > secLargest) {
  36. secLargest = n;
  37. }
  38. }
  39. System.out.println("The largest value is " + largest);
  40. System.out.println("The second largest value is " + secLargest);
  41.  
  42. }
  43. }
Success #stdin #stdout 0.04s 4386816KB
stdin
9
8
5
10
10
0
stdout
This program finds the two largest integers in a list.
Enter values, one per line, using a 0 to signal the end of list.
The largest value is 10
The second largest value is 10