Question

In: Computer Science

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 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 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

please don't copy the same code as the one uploaded in the chegg

try to write something simple and understandable

please explain every statement please, its for project and i don't want to have similar project like one once in chegg. it would be much apperciated

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

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

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

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...
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...
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...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. 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...
simple Java project// please explain every statement with reasoning. Thank you Read from a file that...
simple Java project// please explain every statement with reasoning. Thank you 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...
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in English (dictionary), to find the number of misspelled words in a story file called Alcott-little-261.txt. Please note that the dictionary words are all lower-cased and there are no punctuation symbols or numbers. Hence it is important that you eliminate anything other than a-z and A-Z and then lower case story words for valid comparisons against the WordList file. When you read each line of...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in English (dictionary), to find the number of misspelled words in a story file called Alcott-little-261.txt. Please note that the dictionary words are all lower-cased and there are no punctuation symbols or numbers. Hence it is important that you eliminate anything other than a-z and A-Z and then lower case story words for valid comparisons against the WordList file. When you read each line of...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT