Question

In: Computer Science

The input file Each line of the input file will contain a sentence with words separated...

The input file

Each line of the input file will contain a sentence with words separated by one space. Read a line from the listed below  and use a StringTokenizer to extract the words from the line.

The input file

.

Mary had a little lamb
whose fl33ce was white as sn0w
And everywhere that @Mary went
the 1amb was sure to go.
Read the above  that contains a paragraph of words. 


Put all the words in an array, put the valid words (words that have only letters) in a second array, 
and put the invalid words in a third array. Sort the array of valid words using Selection Sort. 
Write a java program Create a GUI to display the arrays using a GridLayout with one row and three columns.

please explain the code properly and give reassnong

An example of the input file would be:

Mary had a little lamb
Whose fl33ce was white as sn0w.

file name

WordGUI.java

Solutions

Expert Solution

Solution :

import java.util.*;

class Main {
   public static void main (String[] args) {
   String str="Mary had a little lamb whose fl33ce was white as sn0w And everywhere that @Mary went the 1amb was sure to go";
StringTokenizer stk = new StringTokenizer(str, " "); //Using a String tokenizer
LinkedList<String> l1=new LinkedList<String>(); //Since we don not know the size of arrays, therefore I used linked list for beter efficiency
LinkedList<String> l2=new LinkedList<String>();
while(stk.hasMoreTokens())
{
String s=stk.nextToken();
if(s.contains("1")||s.contains("2")||s.contains("3")||s.contains("4")||s.contains("5")||s.contains("6")||s.contains("7")||s.contains("8")||s.contains("9")||s.contains("0")||s.contains("@")||s.contains("&")||s.contains("#")||s.contains("$"))
{
l2.add(s);
}
else
l1.add(s);
}

   System.out.println("First array: ");
   for(int i=0;i<l1.size();i++)
   {
   System.out.print(l1.get(i)+" ");
   }
       System.out.println("\nSecond array: ");
   for(int i=0;i<l1.size();i++)
   {
   System.out.print(l2.get(i)+" ");
   }
   }
}

Output of above cod eis shown below :


Related Solutions

A hotel salesperson enters sales in a text file. Each line contains the following, separated by...
A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Write a program that reads such a file and displays the total amount for each service category. Use the following data for your text file:5 pts Bob;Dinner;10.00;January 1, 2013 Tom;Dinner;14.00;January 2, 2013 Anne;Lodging;125.00;January 3, 2013 Jerry;Lodging;125.00;January...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input...
c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input begins with an integer indicating the number of word replacement pairs (original and replacement) that follow. The next line of input begins with an integer indicating the number of words in the sentence that follows. Any word on the original list is replaced. Ex: If the input is: 3 automobile car manufacturer maker children kids 15 The automobile manufacturer recommends car seats for children...
assignment in C I have a file that contains a lot of lines with words separated...
assignment in C I have a file that contains a lot of lines with words separated by spaces ( also contains empty lines as well). I need to read this file line by line and put each word into 2d array. NOTE: i need to skip spaces as well as empty lines. also I need to count each word.
Write a program that will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Write function words() that takes one input argument—a file name—and returns the list of actual words...
Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
Write a program that reads a file line by line, and reads each line’s tokens to...
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT