Created
May 7, 2012 19:43
-
-
Save tgruber5150/2629908 to your computer and use it in GitHub Desktop.
I refactored the alphabet program so a separate method is easy to copy for other applications. Feel free to use this program, or the separate method; labled 'addOrderAppendage'.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class Alphabet { | |
static String[] fields = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", | |
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", | |
"u", "v", "w", "x", "y", "z"}; | |
public static void main(String[] args) { | |
String letter = null; | |
letter = pickALetter(letter); | |
printTheLetter(letter); | |
} | |
public static String pickALetter(String l) | |
{ Scanner in = new Scanner (System.in); | |
System.out.print ("Enter a Letter to find out what order it's in: "); | |
l = in.next(); | |
l = l.toLowerCase(); | |
return l; | |
} | |
private static void printTheLetter(String letter) { | |
for (int i=0; i < fields.length; i++ ) { | |
int j = i+1; | |
if (letter.equals(fields[i])) { | |
System.out.print("The letter " + fields[i] + " is the "); | |
System.out.println(+ j + addOrderSuffix(j) + " letter in the alphabet."); | |
} | |
} | |
} | |
static String addOrderSuffix(int i) { | |
if ((i % 10 == 1) & (i != 11)) { | |
return "st"; | |
} else if ((i % 10 == 2) & (i != 12)) { | |
return "nd"; | |
} else if ((i % 10 == 3) & (i != 13)) { | |
return "rd"; | |
} else { | |
return "th"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment