mrsw
mrsw

Reputation: 175

Identifying each word in a file

Importing a large list of words and I need to create code that will recognize each word in the file. I am using a delimiter to recognize the separation from each word but I am receiving a suppressed error stating that the value of linenumber and delimiter are not used. What do I need to do to get the program to read this file and to separate each word within that file?

public class ASCIIPrime {
    public final static String LOC = "C:\\english1.txt";

    @SuppressWarnings("null")
    public static void main(String[] args) throws IOException {

        //import list of words 
        @SuppressWarnings("resource")
        BufferedReader File = new BufferedReader(new FileReader(LOC)); 

        //Create a temporary ArrayList to store data
        ArrayList<String> temp = new ArrayList<String>();

        //Find number of lines in txt file
        String line;
        while ((line = File.readLine()) != null)
        {
            temp.add(line);
        }

        //Identify each word in file
        int lineNumber = 0; 
        lineNumber++;
        String delimiter = "\t";

        //assess each character in the word to determine the ascii value 
        int total = 0; 
        for (int i=0; i < ((String) line).length(); i++)
        {
            char c = ((String) line).charAt(i);
            total += c;
        }

        System.out.println ("The total value of " + line + " is " + total); 
    }
}

Upvotes: 1

Views: 451

Answers (3)

Pimgd
Pimgd

Reputation: 6043

This smells like homework, but alright.

Importing a large list of words and I need to create code that will recognize each word in the file. What do I need to do to get the program to read this file and to separate each word within that file?

You need to...

  • Read the file
  • Separate the words from what you've read in
  • ... I don't know what you want to do with them after that. I'll just dump them into a big list.

The contents of my main method would be...

BufferedReader File = new BufferedReader(new FileReader(LOC));//LOC is defined as class variable

//Create an ArrayList to store the words
List<String> words = new ArrayList<String>();

String line;
String delimiter = "\t";
while ((line = File.readLine()) != null)//read the file
{
    String[] wordsInLine = line.split(delimiter);//separate the words
    //delimiter could be a regex here, gotta watch out for that
    for(int i=0, isize = wordsInLine.length(); i < isize; i++){
        words.add(wordsInLine[i]);//put them in a list
    }
}

Upvotes: 1

vedi0boy
vedi0boy

Reputation: 1040

I used this great tutorial from thenewboston when I started off reading files: https://www.youtube.com/watch?v=3RNYUKxAgmw

This video seems perfect for you. It covers how to save file words of data. And just add the string data to the ArrayList. Here's what your code should look like:

import java.io.*;
import java.util.*;

public class ReadFile {

    static Scanner x;
    static ArrayList<String> temp = new ArrayList<String>();

    public static void main(String args[]){
        openFile();
        readFile();
        closeFile();
    }

    public static void openFile(){
        try(
            x = new Scanner(new File("yourtextfile.txt");
        }catch(Exception e){
            System.out.println(e);
        }
    }

    public static void readFile(){
        while(x.hasNext()){
            temp.add(x.next());
        }
    }

    public void closeFile(){
        x.close();
    }
}

One thing that is nice with using the java util scanner is that is automatically skips the spaces between words making it easy to use and identify words.

Upvotes: 0

Rafael
Rafael

Reputation: 2676

You can use the split method of the String class

String[]    split(String regex) 

This will return an array of strings that you can handle directly of transform in to any other collection you might need.

I suggest also to remove the suppresswarning unless you are sure what you are doing. In most cases is better to remove the cause of the warning than supress the warning.

Upvotes: 0

Related Questions