Question

In: Computer Science

QUESTION : Read from a file that contains a paragraph of words. Put all the words...

QUESTION : Read from a file 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. Create a GUI to display the arrays using a GridLayout with one row and three columns.

The input file Each line of the input file will contain a sentence with words separated by one space. Read a line from the file and use a StringTokenizer to extract the words from the line. An example of the input file would be:

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

I need a JAVA CODE for this

Please have comments for the code, im simply trying to understand this question.

Solutions

Expert Solution

JAVA CODE:

Filename - WordGUI.java

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class WordGUI extends JFrame {
  
   JTextArea masterArea;   //where all the words are present
   JTextArea validArea;    //where only valid words will be present
   JTextArea invalidArea;  //where only invalid words will be present
  
   public WordGUI() {
       this.setSize(500, 500);
       this.setTitle("Project 1");
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
       this.setLayout(new GridLayout(1,3));   //setting a grid layout
      
       masterArea = new JTextArea();  //(as they are suppose to store text in those respective fields)
       validArea = new JTextArea();
       invalidArea = new JTextArea();
      
       JScrollPane scrollPane1 = new JScrollPane();
       JScrollPane scrollPane2 = new JScrollPane();
       JScrollPane scrollPane3 = new JScrollPane();
      
       scrollPane1.setViewportView(masterArea);
       scrollPane2.setViewportView(validArea);
       scrollPane3.setViewportView(invalidArea);
      
       this.add(scrollPane1);
       this.add(scrollPane2);
       this.add(scrollPane3);
      
       this.setVisible(true);
   }
  
   public void addtoMasterArea(String str) {
       masterArea.append(str);
   }
   public void addtoValidArea(String str) {
       validArea.append(str);
   }
   public void addtoInvalidArea(String str) {
       invalidArea.append(str);
   }
      
}

Filename - Project1.java:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

/**
* In this project we will read a file that contains a paragraph of words.
* We put all the words into an array, then all the valid words (only letters) in a second, and invalid in a 3rd
* We sort the valid array using selection sort
* Then create a GUI with a GridLayout (1,3) to display it
*
*/

public class Project1 {
  
   public static void main(String[] args) {
      
       String[] Word = new String[800];
       int wordCount = 0;
       String[] validWords = new String[800];
       int validWordsCount = 0;
       String[] invalidWords = new String[800];
       int invalidWordsCount = 0;
  
       try {
           //takes the input named Project1Input.txt
           File file = new File("Project1Input.txt");
           BufferedReader br = new BufferedReader(new FileReader(file));
           String line = br.readLine();
          
           // while the line isn't null, it will create a new Tokenizer
           while(line != null) {
               StringTokenizer tokenizer = new StringTokenizer(line);
               //While they're more words it'll add it into the array
               while (tokenizer.hasMoreTokens()) {
                   Word[wordCount] = tokenizer.nextToken();
                   wordCount++;
               }
               line = br.readLine();
           }
           br.close();
          
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       catch (IOException e) {
           e.printStackTrace();
       }
      
       // Filling in the valid array and the invalid array
       // Starts to go through the first array
       for (int i = 0; i < wordCount; i++) {
           // Begins the validity check of every word to check every character inside the string
           Boolean isvalid = true;
           for (int j = 0; j < Word[i].length(); j++) {
               // If the character isn't a string isn't a letter, it adds it into the invalid array
               if (!Character.isLetter(Word[i].charAt(j))) {
                   isvalid = false;
                   break;
               }
           } // If the String is indeed filled with all characters, it will put it into the valid array
           if (isvalid) {
               validWords[validWordsCount++] = Word[i];
           }
           else {
               invalidWords[invalidWordsCount++] = Word[i];
           }
       }
      
       // Selection Sort
       int lowestCount = 0;
       // for every word in the array, it looks for other words behind it that are alphabetically smaller (ASCII)
       // then swap the two places so that the smallest one becomes the lowest index
       for (int i = 0; i < validWordsCount - 1 ; i++) {
           //starting
           lowestCount = i;
           for (int j = i; j < validWordsCount; j++) {
           // current word
               if(validWords[j].compareToIgnoreCase(validWords[lowestCount]) < 0) {
                   lowestCount = j;
               }
           }
          
       String temp = validWords[i];
       validWords[i] = validWords[lowestCount];
       validWords[lowestCount] = temp;
      
       }
       WordGUI gui = new WordGUI();
      
       //appending each text area
       for (int i=0; i < wordCount; i++) {
           gui.addtoMasterArea(Word[i] + "\n");
       }
       for (int i=0; i < validWordsCount; i++) {
           gui.addtoValidArea(validWords[i] + "\n");
       }
       for (int i=0; i < invalidWordsCount; i++) {
           gui.addtoInvalidArea(invalidWords[i] + "\n");
       }
   }
}

Note: If you have any doubts, or facing any type of issues, feel free to ask

I'll always respond to you no matter how busy I am.

Best of luck for your future.


Related Solutions

Read from a file that contains a paragraph of words. Put all the words in an...
Read from a file 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. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with words separated...
JAVA PROJECT CREATING GUI WITH ARRAY Read from a file that contains a paragraph of words....
JAVA PROJECT CREATING GUI WITH ARRAY Read from a file 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. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER IS PREFERRED THANK YOU IN ADVANCE...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
How to read the given structure from a random CSV file separated by commas(which contains no...
How to read the given structure from a random CSV file separated by commas(which contains no headers only the values of the contents of the structure) and then insert in a binary search tree using one of the structure contents as a key i.e. datetime and handle duplicates in binary search tree by implementing link_list.Please develop a C code for this. struct data{ char biker_id[200]; char distance_bike_travelled[200]; char datetime[200]; char count_tripr[200]; }
Choose from the following list of words to complete the paragraph below. Not all terms will...
Choose from the following list of words to complete the paragraph below. Not all terms will be used, and none of them can be used more than once. positive     intermediate     free   negative   substrate   product   activation   thermal An enzyme reduces the_____________ energy of a reaction, causing it to occur more frequently at a given temperature than it would otherwise. This does not alter the________ energy of the reactants or products. If the ΔG of a reaction is____________ , the reaction is spontaneous....
Edit question Miscellaneous C knowledge. (Put all answers in the same q5.txt file, they’re all short.)...
Edit question Miscellaneous C knowledge. (Put all answers in the same q5.txt file, they’re all short.) (a) [2 marks] On an old 16-bit computer system and its C compiler,sizeof(double)=8andsizeof(int)=2. Given the two type definitions below, what weresizeof(s)andsizeof(lingling)on that system? Assume that ins, there is no gap between the twofields. typedef struct s { double r[5];int a[5]; } s; typedef union lingling { double r[5];int a[5]; } lingling; (b) [2 marks] Given the following declarations, two questions: What is the type...
Read the words in from the binary file and figure out how many times each word...
Read the words in from the binary file and figure out how many times each word appears in the file. Display the results to the user. Use ObjectInputStream to read binary file Use a HashMap with the word as a key (String) and an Integer as the value. For each word, first check to see if it already exists in the Map. If not, add the word as key with a value of 1 for the Integer value. If it...
answer the question in a paragraph of 200 words. Distinguish stocks from bonds and discuss the...
answer the question in a paragraph of 200 words. Distinguish stocks from bonds and discuss the advantages and disadvantages of owning each type of financial instrument.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT