import java.rmi.UnexpectedException; import javax.swing.JOptionPane; /** *
Using arrays in conjunction with Strings (which appear as arrays of chars) to some * interesting (and maybe useful) things.
* @author Tom R. * */ public class StringArrayOps { /**Used by the various internal routines to distinguish types of "letters".
*/ private static final char[] VOWELS = {'a','e','i','o','u'}; /**Preconditions: None.
*
* PostConditions: returns the number of vowels in a String.
*
String)
* @return int >= 0.
*/
public static int countVowels( String str ) {
int numberOfVowels = 0;
return numberOfVowels;
}
/**
* Preconditions: None.
*
* Postconditions: Returns the number of times that the given char ch appears in the
* String str.
*
int >= 0
*/
public static int countChar( char the_char, String str ) {
int number_of_times_found = 0;
return number_of_times_found;
}
/**
* Preconditions: None.
*
* Postconditions: Returns the number of consonants in a String.
*
* @param str (String)
* @return int >= 0
*/
public static int countConsonants( String str ) {
int number_of_consonants = 0;
return number_of_consonants;
}
// entry point
public static void main( String[] args ) {
String str = JOptionPane.showInputDialog( null, "Enter an arbitary String: " );
int number_of_vowels = countVowels(str);
JOptionPane.showMessageDialog( null, "Your String contained " + number_of_vowels + " vowels, and "
+ countConsonants(str) + " consonants." );
/* Notice what's going to happen here ... I'm using the try-catch
* statements below to verify that the user entered only one character.
* I don't really need to do this ... I could just ignore any extra
* characters and accept the first, but doing so doesn't tell the user
* what's happened in the event they were expecting something different.
*/
char[] the_character_array=null; // ask yourself: why are these variables
String the_char=null; // declared here and not within the try { }?
try {
the_char = JOptionPane.showInputDialog(
null,
"Enter a character\n and find out " +
"how many times\nthe character appeared within the original String." );
/* convert user input into an array of chars ... which we hope contains
* only one!
*/
the_character_array = the_char.toCharArray();
// If the character array does contain more than one char, complain ....
if( the_char.length() > 1 ) {
throw new UnexpectedException("Too many chars" );
}
if( the_character_array.length < 1 )
throw new NullPointerException();
} catch( UnexpectedException ue ) {
/* Upon a complaint, inform ther user. */
JOptionPane.showMessageDialog( null, "You entered more than 1 char; extras ignored!",
"Too many chars entered", JOptionPane.ERROR_MESSAGE );
} catch( NullPointerException npe) {
JOptionPane.showMessageDialog(null, "You must enter a character ...", "Missing Input", JOptionPane.ERROR_MESSAGE );
} finally {
/* note the order of operations here: this will be done at the end of
* dialogue ...
*/
if( the_character_array.length < 1 ) {
JOptionPane.showMessageDialog( null, "Nothing entered ...", "Missing Input", JOptionPane.INFORMATION_MESSAGE );
return;
}
JOptionPane.showMessageDialog(null,
"Your character appeared " + countChar(the_character_array[0], str ) + " times.");
return;
}
}
}