Question

In: Computer Science

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 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
And everywhere that @Mary went
the 1amb was sure to go.

Submitting the Project.
You should have two files to submit for this project:
Project1.java
WordGUI.java

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

Output:

input.txt

Mary had a little lamb
whose fl33ce was white as sn0w
And everywhere that @Mary went
the 1amb was sure to go.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Project.java

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

class Project1 {

  public static void main(String args[]) {
    File file = null;
    FileReader fr = null;
    BufferedReader br = null;
    String line;
    StringTokenizer st = null;

    int numberOfWordsInFile;

    String words[];
    int index;

    String validWords[];
    int validWordsIndex;

    String invalidWords[];
    int invalidWordsIndex;
    try {
      file = new File("input.txt");
      fr = new FileReader(file);
      br = new BufferedReader(fr);
      numberOfWordsInFile = 22;
      words = new String[numberOfWordsInFile];
      index = 0;
      while ((line = br.readLine()) != null) {
        st = new StringTokenizer(line);
        while (st.hasMoreElements()) {
          words[index] = (String) st.nextElement();
          index++;
        }
      }
      fr.close();
      validWords = new String[numberOfWordsInFile];
      validWordsIndex = 0;
      invalidWords = new String[numberOfWordsInFile];
      invalidWordsIndex = 0;
      for (int i = 0; i < words.length; i++) if (
        (!words[i].equals("")) &&
        (words[i] != null) &&
        (words[i].matches("^[a-zA-Z]*$"))
      ) {
        validWords[validWordsIndex] = words[i];
        validWordsIndex++;
      } else {
        invalidWords[invalidWordsIndex] = words[i];
        invalidWordsIndex++;
      }
      selectionSort(validWords, validWordsIndex);
      WordGUI wg = new WordGUI();
      wg.display(
        words,
        index,
        validWords,
        validWordsIndex,
        invalidWords,
        invalidWordsIndex
      );
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  static String[] selectionSort(String[] validWords, int validWordsIndex) {
    for (int i = 0; i < validWordsIndex - 1; i++) {
      int min_index = i;
      String minStr = validWords[i];
      for (int j = i + 1; j < validWordsIndex; j++) {
        if (validWords[j].compareTo(minStr) < 0) {
          minStr = validWords[j];
          min_index = j;
        }
      }
      if (min_index != i) {
        String temp = validWords[min_index];
        validWords[min_index] = validWords[i];
        validWords[i] = temp;
      }
    }
    return validWords;
  }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

WordGUI.java

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

class WordGUI {

  public void display(
    String[] words,
    int index,
    String[] validWords,
    int validWordsIndex,
    String[] invalidWords,
    int invalidWordsIndex
  ) {
    JFrame f = new JFrame();
    f.setLayout(new GridLayout(1, 3));

    String label = "";

    label = convertArrayToLabel(words, index);
    f.add(new JLabel(label));

    label = convertArrayToLabel(validWords, validWordsIndex);
    f.add(new JLabel(label));

    label = convertArrayToLabel(invalidWords, invalidWordsIndex);
    f.add(new JLabel(label));

    f.setSize(1000, 1000);
    f.setVisible(true);
  }

  public String convertArrayToLabel(String[] array, int maxIndex) {
    String label = "<html>";
    for (int i = 0; i < maxIndex; i++) {
      label = label + array[i] + "<br/>";
    }
    label = label + "</html>";
    return label;
  }
}


Related Solutions

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...
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 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...
● 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...
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]; }
Read an unsorted keywords file once to determine how many words are in the file. Allocate...
Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check your sorted array...
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....
Done in c++ Read this file one time to determine how many records it contains. Be...
Done in c++ Read this file one time to determine how many records it contains. Be sure to check for a successful file open here. Close the file. Allocate memory dynamically to store the data from step 1. This dynamically allocated memory should be for an array of strings. The array size should be exactly the number of records from step 1. Reopen the file and read it a second time, storing each record into the array of strings from...
c++ Please read the instructions carefully. You must put the proper items in the proper file...
c++ Please read the instructions carefully. You must put the proper items in the proper file (eitehr SSandwich.h or SSandwich.cpp. Do NOT include any main function in your submission. You are encouraged to write your own main function to test what you are submitting. You are submit two files via BlackBoard: A file named SSandwich.h, a header file defining the class SSandwich. No inline methods are permitted. Any enum classes should be defined here also. Your header file should have...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT