Question

using java create hash set that can

(a) Read one word from the file. (b) Remove all non-alphanumeric characters from the word. A non-alphanumeric character is an

for the file use a txt file: Hi my name is rick.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program: In this program, we read a text file from the system, read each word of each line, check if a word contains any non-alphanumeric characters, if it does then we remove it from the word and add it to the hash set. At the end, we print our hash Set to check our program.

Note: HashSeet contains elements in random order, for sorted order we can use a TreeSet,

Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class CreateHash {

    public static void main(String[] args) {
        //Create a hash set
        Set<String> wordSet = new HashSet<>();

        //Create File class instance, pass name of text file as input
        File file = new File("input.txt");

        //Use a try catch block to catch File not found exception
        try {
            //Read the file, pass file instance as input to scanner class object
            Scanner read = new Scanner(file);

            //Read each line of the text file
            while (read.hasNextLine()) {

                //Store the current line
                String line = read.nextLine();

                //Create an array of words in the current line, by splitting the line using space as delimiter
                String[] words = line.split(" ");

                //Read each word
                for (String word : words) {

                    //Create a loop till the length of word
                    for (int i = 0; i < word.length(); i++) {

                        //Store current character
                        char curr = word.charAt(i);

                        //Check if its alphanumeric or not
                        if (!Character.isLetterOrDigit(curr)) {

                            //If it is not, remove current character from the word using substring function
                            word = word.substring(0, i) + word.substring(i + 1);

                            //decrement the index as length of string has reduced,
                            //there might be continuous non-alphanumeric characters
                            i--;
                        }
                    }

                    //After operating the word, add it to the set
                    wordSet.add(word);

                }
            }
            read.close();

            //Print the set of words
            System.out.println(wordSet);

        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        }
    }
}

2 3 Fimport java.io.File; import java.io.FileNotFoundException; import java.util.HashSet; import java.util.Scanner; import ja

46 //there might be continuous non-alphanumeric characters i--; 48 A 49 50 51 52 53 //After operating the word, add it to the

Text File (input.txt):

Hi my name is rick.

Output:

[Hi, name, is, rick, my] Process finished with exit code o

Add a comment
Know the answer?
Add Answer to:
using java create hash set that can for the file use a txt file: Hi my...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ and not Java: Implement a spelling checker by using a hash table. Assume that...

    In C++ and not Java: Implement a spelling checker by using a hash table. Assume that the dictionary comes from two sources: an existing large dictionary and a second file containing a personal dictionary. Output all misspelled words and the line numbers on which they occur. Also, for each misspelled word, list any words in the dictionary that are obtainable by applying any of the following rules: 1. Add one character. 2. Remove one character. 3. Exchange adjacent characters The...

  • JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following...

    JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following text: MWTaahyiebt_e,c__hnyaoontuc;'e_rste_r_aynr_oert_e_gasoduoipdnp_got_shoeandtl__yty_oot_uhrree__apTdrH_oItgRhrDia_sml__eowtnotere.kr_ss_. Inside puzzle2.txt, write the following text: WTTohhriikssi__niigss,___ttbhhueet___wryrioogunh'gtr__emm_eessshssoaawggieen__gff_rrtoohmme___sswaarmmoppnllgee_22o..nttexxstt Open a file specified by the user. This file will contain a bunch of characters. You should read in each character from the file, one character at a time. Display every third character on the screen. Throw the other characters away. There is a sample input file called puzzle.txt, containing a little message you can...

  • create a hash table to implement spell checker in java 1. Create a file of 100...

    create a hash table to implement spell checker in java 1. Create a file of 100 words of varying length (max 8 characters). 2. All the words are in lower case. 3. design a hash table. 4. enter each word in the hash table. Once the hash table is created, run it as a spell checker.enter a word (interactive), find the word in the hash table. If not found enter an error message. Then prompt for next word. End the...

  • Write a spell checker that stores a set of words, W, in a hash table and...

    Write a spell checker that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a spell check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, because it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to spellCheck(s) returns a...

  • JAVA PROGRAMMING File Name You can not just take the file and send it. When Polycarp...

    JAVA PROGRAMMING File Name You can not just take the file and send it. When Polycarp trying to send a file in the social network "Codehorses", he encountered an unexpected problem. If the name of the file contains three or more "x" (lowercase Latin letters "x") in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed. Determine the...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

  • Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File...

    Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File encryption is the science of writing the contents of a file in a secret code. Write an encryption program that works like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code....

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • You're to write a C++ program that analyzes the contents of an external file called data.txt....

    You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT