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

c++ The input items are given in one line and each item is separated by white...
c++ The input items are given in one line and each item is separated by white spaces (blank-space and tab). how can I implement? and input: 34 peach 7 output: number: 34 7 string: peach How can I separate the numbers and characters that I type at once?
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...
Write a python program that calculates the average number of words per sentence in the input...
Write a python program that calculates the average number of words per sentence in the input text file. every sentence ends with a period (when, in reality, sentences can end with !, ", ?, etc.) and the average number of sentences per paragraph, where a paragraph is any number of sentences followed by a blank line or by the end of the text.
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.
Make a program to read a file, filled with words separated by new lines, from command...
Make a program to read a file, filled with words separated by new lines, from command line arguments (args[0]), print the file, and then add each word into an arraylist. After making the arraylist, iterate through it and print its contents.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT