In: Computer Science
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
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 :