Class StringStuff

Object
extended byStringStuff

public class StringStuff
extends Object

Utility class for Strings.

Implement each method.  Whenever you write a method, write a small driver (main() method) to test it and make sure that it works.  You should also show it to me at that point.

Author:
YOU

Method Summary
static String acronym(String str)
          Creates an acronym out of a string.
static char chosenLetter(String choices)
          Gets the user's choice of a letter.
static int countLetter(String str, char letter)
          Counts how many occurences of a certain letter are in a given String.
static String firstWordOf(String str)
          Returns the first word of a string
static String FLToLF(String name)
          Converts a name from first name last name format to last name, first name format.
static boolean inOrder(String str1, String str2)
          Determines whether two strings are in lexicographical order.
static String LFToFL(String name)
          Converts a name from last name, first name format to first name last name format.
static void printWords(String str)
          Prints all of the words in a String on seperate lines.
static String reverseString(String str)
          Reverses the order of the letters in a string
static String rotateOne(String str)
          Takes a string and shifts every letter one position to the left, moving the first letter to the end.
static void scrollMessage(String str)
          Prints a string like a rotating banner.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

 

Method Detail

rotateOne

public static String rotateOne(String str)
Takes a string and shifts every letter one position to the left, moving the first letter to the end.

Example: If the input is "Rotate Me " the return value will be "otate Me R"

Parameters:
str - the string to rotate
Returns:
the resulting string after the rotation

scrollMessage

public static void scrollMessage(String str)
Prints a string like a rotating banner. It is reprinted once every second with all of the letters shifted one position to the left, and the first letter moved to the end.

Example: If the input is "Rotate Me" the output will look like:

 Rotate Me
otate Me R
tate Me Ro
ate Me Rot
te Me Rota
e Me Rotat
Me Rotate
Me Rotate
e Rotate M
Rotate Me
Rotate Me
...and so on.
Parameters:
str - the text for the rotating banner

reverseString

public static String reverseString(String str)
Reverses the order of the letters in a string
Parameters:
str - the string to reverse
Returns:
the resulting string after the reversal

firstWordOf

public static String firstWordOf(String str)
Returns the first word of a string
Parameters:
str - the string to find a word in
Returns:
a string containing all of the letters of a string that precede the first space character

chosenLetter

public static char chosenLetter(String choices)
Gets the user's choice of a letter.

Sample run (with input parameter of "ABC"):
Choose one of the following letters:
A, B, C
Your choice?
Z
Invalid choice. Your choice? B

'B' is returned.

Parameters:
choices - a String containing characters that represent valid letter choices
Returns:
the letter the user chooses

countLetter

public static int countLetter(String str,
char letter)
Counts how many occurences of a certain letter are in a given String.

Example: If the inputs are "occurences" and 'c' it will return 3

Parameters:
str - the string to search
letter - the letter to search for
Returns:
how many times letter appears in str

printWords

public static void printWords(String str)
Prints all of the words in a String on seperate lines. Words are seperated by space characters in the input parameter.

Example: If the input is "java programming is fun" the output will be:

 java
programming
is
fun

Parameters:
str - a String containing words seperated by spaces

acronym

public static String acronym(String str)
Creates an acronym out of a string. The acronym is created by concatenating the first letters of every word in the String, then converting the result to upper case. Words are seperated by space characters.

Example: If the input is "Sun Microsystems, Incorporated" the return value will be "SMI"

Parameters:
str - the String to make an acronym of
Returns:
the acronym

FLToLF

public static String FLToLF(String name)
Converts a name from first name last name format to last name, first name format.

Example: If the input is "John Doe" the return value will be "Doe, John"

Parameters:
name - the name, with the first name before the last name, with a space between them. The first name may contain a space, the last name may not.
Returns:
a string containing the last name, a comma, and space and the first name from the input parameter

LFToFL

public static String LFToFL(String name)
Converts a name from last name, first name format to first name last name format.

Example: If the input is "Doe, John" the return value will be "John Doe"

Parameters:
name - the name, with the last name followed by a comma, a space, and the first name.
Returns:
a string containing the first name, a space, and the last name

inOrder

public static boolean inOrder(String str1,
String str2)
Determines whether two strings are in lexicographical order. Lexicographical order is an extension of alphabetical ordering that includes rules for ordering other characters like numbers and symbols.
Parameters:
str1 - the first string
str2 - the second string
Returns:
true if str1 comes before str2 or is the same as str2. Returns false if str1 comes after str2.