Hi I am currently working on a project where I have to find the most frequently occurring word in an imported file. I have imported the file and can display what it says, but Im having trouble with this part: "Create a Word class that has a string and a count. Use a LinkedList
of Word objects. When you read a word from the novel check if it is in the list. If so increment the count. If not add it with count 1. After processing all of the words find the word with the highest count."
Here's my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;
public class mostFrequentWord 
{
	public void Read()
	throws FileNotFoundException
	{
		Scanner input = new Scanner(System.in);
		FileReader reader = new FileReader("c:\\cecs\\cecs274\\test.txt");
		Scanner in = new Scanner(reader);
		in.useDelimiter(" [ ^ a - zA - Z ] + ");
		while (in.hasNext())
		{
			String s = in.next().toLowerCase();
			System.out.println(s);
		}
		
	}
}

Here's the entire project if you're having trouble understanding what i said:
Use the Java LinkedList and ListIterator classes. You will read the text
of the novel Emma by Jane Austen and find the most frequently occurring
word. Convert to lowercase because we do not want "The" and "the", etc. to be considered
different. Here is the code to read the file and get the words
Scanner in = new Scanner(new File("emma.txt"));
in.useDelimiter("[^a-zA-Z]+");
while (in.hasNext()){
String s = in.next().toLowerCase();
....
}
When done with the scanner, execute in.close(). Import File and
FileNotFoundException from the java.io package. Whichever method creates
the File need to add "throws FileNotFoundException" after the () for
parameters and the { to start the body of the method. For the code I have
shown above, emma.txt is in the same folder as the program. (In Eclipse
there may be an addition folder on the path that needs to be added.)

Create a Word class that has a string and a count. Use a LinkedList
of Word objects. When you read a word from the novel check if it is in the
list. If so increment the count. If not add it with count 1. After processing
all of the words find the word with the highest count. (You do not need to
check for ties.)

Remember that adding to the list directly adds at the end which is not
what we want. Adding at the list iterator cursor will add just after the
last item returned by next(), but you could use previous() to get to the
position before the item returned.

Boundary conditions require careful thinking. For example how will you
handle an empty list, a word that is alphabetically less than any previous
word, or a word that is alphabetically greater than any previous word.

Time your code (for comparison with other methods).
System.currentTimeMillis() returns the current time in milliseconds since
Jan. 1, 1970 as a value of type long. Get this value at the end of
execution and at the beginning and subtract to get the elapsed time.

For your test run output the number of items in the list, the highest
freqency word with its frequency, and the time taken.

emma.txt is on the course site. When developing the program using a small
file of a few lines at most and only use Emma when you are satisfied that
your program works.

Steps to do your project:

1. Make a main class (You've already done this and posted it).
2. Make a class called Word.
3. The 'Word' class should have two instance variables: 'String theWord' and 'int count'. The 'Word' class should have a constructor that takes a String and an int and assigns them to theWord and count.
4. The 'Word' class should also have an isEqual method that returns true if the word passed in is the same as the String 'theWord' and returns false otherwise. To do this you can use the String class's equals method (just FYI, an equalsIgnoreCase method also exists, but since your teacher already converted toLowerCase it doesn't matter).
5. For your main class (mostFrequentWord) you need to have an instance variable 'LinkedList wordList'. Each time you read in a new word from the text file, you need to search the entire wordList. If the word is already in the wordList, you add 1 to its count. If the word in not in the wordList, you create a new Word Object using the constructor I mentioned earlier, and you pass in the word and a count of 0. To 'search the entire wordList' you need to use the method your teacher was talking about earlier: you need to use an iterator:

//Pseudocode
for each item in the list:
if (item.equals(currentWord))
increment that item's count
else make a new Word Object with count 0.

On second thought, after reading more of your teacher's project description, he/she already gave you basically the exact steps required to do this project. What step, exactly, are you having trouble with? And a step isn't "I can't do this" its very specific: for example, can you not iterate over the list? Do you not know how to figure out if an element is equal to another element? (Which I already told you). Etc.

well I knew at this linked list stuff and im having trouble. Do i need to put the whole text file into a linked list or....

alright this is my word class. i jus wanna know if im doing everything right before a go on.

public class Word 
{
	private String toWord;
	private int Count;
	
	public void assign(int count, String word)
	{
		toWord = word;
		Count = count;
	}
	public boolean isEqual()
	{
		if(?)
		{
			return true;
		}
		else
			return false;
	}
	

}

On second thought, after reading more of your teacher's project description, he/she already gave you basically the exact steps required to do this project. What step, exactly, are you having trouble with? And a step isn't "I can't do this" its very specific: for example, can you not iterate over the list? Do you not know how to figure out if an element is equal to another element? (Which I already told you). Etc.

im just having trouble with creating the word class and i dont understand what it means when he says "use a LinkedList
of Word objects." and how to check if a word from the file is a word on the list

im just having trouble with creating the word class and i dont understand what it means when he says "use a LinkedList
of Word objects." and how to check if a word from the file is a word on the list

Your word class is correct except that where you have the "?" it should use the String class's equals method. For example:

String something = "blah";
String somethingElse = "bleh";
if (something.equals(somethingElse)){
//In your case, you'd return true; in here.
System.out.println("Equal!");
}

As for the LinkedList, your teacher means for you to do this:

LinkedList<Word> list = new LinkedList<Word>();

As for why you put the <Word> in brackets like that, it is called Generics. So if you google Java Generics and read some articles, or if you want to check out these:
http://tutorials.jenkov.com/java-generics/index.html
^ Simpler but slightly worse tutorial
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
^ Better tutorial but not light reading.

Your word class is correct except that where you have the "?" it should use the String class's equals method. For example:

String something = "blah";
String somethingElse = "bleh";
if (something.equals(somethingElse)){
//In your case, you'd return true; in here.
System.out.println("Equal!");
}

As for the LinkedList, your teacher means for you to do this:

LinkedList<Word> list = new LinkedList<Word>();

As for why you put the <Word> in brackets like that, it is called Generics. So if you google Java Generics and read some articles, or if you want to check out these:
http://tutorials.jenkov.com/java-generics/index.html
^ Simpler but slightly worse tutorial
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
^ Better tutorial but not light reading.

okay i did that but how would that find the most common word in the file?

Every time you read in a new word from the file, before creating a new Word object, you should search your LinkedList to see if the word is already in it. If it is already in the LinkedList, then you would just add one to the word's count. If it isn't already in the LinkedList, then you should create a new Word Object with a count of 1.

okay so would it look like this?

String something = "blah";
      String somethingElse = "bleh";
      if (something.equals(somethingElse)){
      //In your case, you'd return true; in here.
      System.out.println("Equal!");
LinkedList<Word> list = new LinkedList<Word>();
}

cuz i get an error

okay here's what i have right now:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;
public class mostFrequentWord 
{
    LinkedList<Word> list = new LinkedList<Word>();
    public void Read()
    throws FileNotFoundException
    {
        Scanner input = new Scanner(System.in);
        FileReader reader = new FileReader("test.txt");
        Scanner in = new Scanner(reader);
        in.useDelimiter(" [ ^ a - zA - Z ] + ");
        while (in.hasNext())
        {
            String s = in.next().toLowerCase();
            System.out.println(s);
        }


    }
}

()

public class Word 
{
    private String toWord;
    private int Count;

    public void assign(int count, String word)
    {
        toWord = word;
        Count = count;
    }
    public void isEqual()
    {
           String something = "blah";
           String somethingElse = "bleh";
           if (something.equals(somethingElse)){
           System.out.println("Equal!");

      }

    }


}

()

import java.io.FileNotFoundException;

public class testmostFrequentWord 
{
    public static void main(String[] args) 
    throws FileNotFoundException 
    {
        mostFrequentWord  m = new mostFrequentWord();
        Word w = new Word();
        m.Read();
        w.isEqual();

    }

}

thats my main, word and tester classes. and i know i have to do what u said I just dont know how to code "Every time you read in a new word from the file, before creating a new Word object, you should search your LinkedList to see if the word is already in it. If it is already in the LinkedList, then you would just add one to the word's count. If it isn't already in the LinkedList, then you should create a new Word Object with a count of 1. " that. Ive been at it for a week and still dont get how to do it

To search a LinkedList you need to use an Iterator. To code an Iterator type "iterator java" into google and read things. To see if one String is equal to another String, use the code I already gave you. In your case, since your 'toWord' is inside your Word class, you'll need to create a getter method that returns the variable. Again, use google if you don't know what a getter method is.

If you simply combine the two ideas I just presented to you, you will easily be able to implement my suggestion.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Word 
{
    public static void main(String[] args)
    {
        List<String> words = new ArrayList<String>();
        List<Integer> counts = new ArrayList<Integer>();
        Scanner sc2 = null;
        try 
        {
            sc2 = new Scanner(new File("input.txt"));
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();  
        }
        while (sc2.hasNextLine()) 
        {
            Scanner s2 = new Scanner(sc2.nextLine());
            boolean b;
            while (b = s2.hasNext()) 
            {
                String s = s2.next();
                //System.out.println(s);
                words.add(s);
            }
        }
        int i=0;
        while(i<words.size()-1)
        {
            String wc=words.get(i);
            //System.out.println(wc);
            int count=0;
            int j=0;
            while(j<words.size()-1)
            {
                if(wc.equals(words.get(j)))
                {
                    count++;
                }
                j++;
            }
            counts.add(count);
            i++;
        }

        i=1;
        int max=counts.get(0);
        while(i<counts.size()-1)
        {
            if(max<counts.get(i))
            {
                max=counts.get(i);
            }
            //System.out.println(counts.get(i));
            i++;
        }
        //System.out.println(max);
        i=0;
        while(i<counts.size()-1)
        {
            if(max==counts.get(i))
            {
                System.out.print("The word is :");
                System.out.println(words.get(i));
                break;
            }
            //System.out.println(counts.get(i));
            i++;
        }
    }
}

Hello salasah
Welcome to DaniWeb, and thank you for taking the time to contribute.

Here are a couple of things to think about before your next post:
1. This thread is 2 years old, nobody is still waiting for the answer
2. More important:
Here at DaniWeb we try to help people learn Java and develop their Java skills. We do NOT do people's homework for them. Your post explains and teaches nothing. In future please help by pointing people in the right direction - eg tell them which classes and methods they should read about. If you feel you should correct someone's code then that's useless if you don't explain what you changed and why you changed it as you did. If you need to explain with actual code then explain why you coded it the way you did. Don't just spoon-feed them a solution to copy and paste.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.