previous | start | next

If statement: Example 2

Find the largest of three input strings. That is, the string that comes last in the natural ordering of strings.

    1   public class Max3 {
    2   
    3    
    4     public static void main(String[] args) {
    5       String s1, s2, s3;
    6       String max;
    7       Scanner input = new Scanner(System.in);
    8   
    9       System.out.println("This program computes the smallest of three input strings");
   10       System.out.println("That is, it prints the string that comes last in the usual " +
   11           "ordering of strings.");
   12       System.out.println();
   13       
   14       System.out.printf("\nFirst string: ");
   15       s1 = input.next();
   16       System.out.printf("\nSecond string: ");
   17       s2 = input.next();
   18       System.out.printf("\nThird string: ");
   19       s3 = input.next();
   20       
   21       max = s1;
   22       if ( s2.compareTo(max) > 0  ) {
   23         max = s2;
   24       }
   25       if (s3.compareTo(max) > 0 ) {
   26         max = s3;
   27       }
   28       ...
   29       
   30     }
   31   }


previous | start | next