string tokenization using java
you will practice taking user input and performing string tokenization. You will also practice regular expressions. You will create two separate programs to perform these tasks, as outlined below.
1. Prompt the user to input a sentence. Take an input sentence from the user with any number of spaces between words, terminated by pressing the enter key. Print each word on a separate line. Finally, print the longest word, along with the total number of vowels in that word.
Example:
Program: Please enter a sentence:
User: Hello there computer
Program: Hello
there
computer
computer has 3 vowels
Program: Please enter a sentence:
User: Here is a sample sentence
Program: Here
is
a
sample
sentence
sentence has 3 vowels
2. Prompt the user to input a name. The name may have a salutation, and valid salutations are “Mr” and “Ms”. Next, after a single whitespace character (if there was a salutation) is the first name. After another whitespace character, there may or may not be a middle initial, which is a single uppercase letter which may or may not have a “.” after it. Finally, after another whitespace, the last name is given. The first and last name will both start with uppercase letters. Determine, using
a single regex check, whether or not the input name is valid. If it is valid, print the first and last name. If not, let the user know.
Example:
Program: Please enter a name:
User: Mr John D Smith
Program: John Smith
Program: Please enter a name:
User: Mr John D. Smith
Program: John Smith
Program: Please enter a name:
User: John Smith
Program: John Smith
Program: Please enter a name:
User: Mr John Smith
Program: John Smith
Program: Please enter a name:
User: John D Smith
Program: John Smith
Program: Please enter a name:
User: Captain Fluffyface the 6th
Program: Invalid name.
Program: Please enter a name:
User: Ms 9Caroline Smith
Program: Invalid name.
Program: Please enter a name:
User: Spongebob Squarepants
Program: Spongebob Squarepants
Answer Part 1
import java.util.*; public class Part1 { public static void main (String[] args){ String input; StringTokenizer myTokens; String Token; String longestWord = ""; String vowels = "aeiouAEIOU"; int numOfVowels = 0; /*Prompt the user to enter a sentence*/ Scanner keyboard = new Scanner(System.in); System.out.print("Please enter a sentence: "); input = keyboard.nextLine(); myTokens = new StringTokenizer(input," "); while(myTokens.hasMoreTokens() != false){ Token = myTokens.nextToken(); System.out.println(Token); /*This code is used to find longest word */ if( Token.length() > longestWord.length()) { longestWord = Token; } } /*This code is used to find vowels */ for(int i = 0; i < longestWord.length(); i++){ for(int j = 0; j < vowels.length(); j++){ if( longestWord.charAt(i) == vowels.charAt(j)){ numOfVowels++; } }// end of inner for } // end of outer for System.out.println("\n"+ longestWord + " has " +numOfVowels +" vowels"); } }
Answer Part 2
import java.util.*; public class Part2 { public static void main (String[] args){ /*initialize variables*/ int numOfTok; String firstName = "",lastName = "",name = "" ; Boolean input = false; /*Prompt the user to enter the name*/ System.out.println("Please enter a name: "); Scanner keyboard = new Scanner (System.in); name = keyboard.nextLine(); /* Get tokens separated by a space */ StringTokenizer tok = new StringTokenizer (name," "); /*Decide how many tokens were entered */ numOfTok = tok.countTokens(); /*Check if the name matches the regular expreskeyboard.close();sion and the name follows a valid format*/ if (name.matches("(Mr|Ms)?( )?[A-Z][a-z]{1,}( )?([A-Z](.)?)?( )[A-Z][a-z]{1,}")){ firstName = tok.nextToken(); if (firstName.matches("(Mr|Ms)")){ /*Next token is the first name*/ firstName = tok.nextToken(); input = true; } /*Next token is the last name*/ lastName = tok.nextToken(); if (numOfTok > 2 && input == false) lastName = tok.nextToken(); if (numOfTok > 3) lastName = tok.nextToken(); /* Output first and last name to the screen*/ System.out.println(firstName + " " + lastName); } else System.out.println("Invalid name"); } }
Leave a reply