From CS160

Main: Lab9Solution

import java.util.Scanner;

public class Lab9 {
	public static void main(String[] args) 
	{
		Scanner scan = new Scanner(System.in);

		System.out.print("Enter 3 strings: ");

		String a = scan.next();
		String b = scan.next();
		String c = scan.next();

		System.out.println("The minimun of the 3 strings is: " + Min3Strings(a,b,c));
		System.out.println("The 3 strings in lexiographic order are: " + Sort3Strings(a,b,c));
	}

	public static String Min3Strings(String a, String b, String c) {

		if(a.compareTo(b) <= 0 && a.compareTo(c) <= 0)
			return a;

		if(b.compareTo(a) <= 0 && b.compareTo(c) <= 0)
			return b;

		if(c.compareTo(b) <= 0 && c.compareTo(a) <= 0)
			return c;

		return null;
	}

	public static String Sort3Strings(String a, String b, String c) {

		if(a.compareTo(b) > 0)
		{
			String temp = a;
			a = b;
			b = temp;
		}

		if(a.compareTo(c) > 0)
		{
			String temp = a;
			a = c;
			c = temp;
		}

		if(b.compareTo(c) > 0)
		{
			String temp = b;
			b = c;
			c = temp;
		}
		return a + " " + b + " " + c;
	}	
}
Retrieved from http://www.cs.colostate.edu/~asa/courses/cs160/fall08/pmwiki/pmwiki.php/Lab9Solution
Page last modified on October 25, 2008, at 12:15 PM MST