
-----------------------------------
Xi_is4COW
Sun Feb 12, 2006 3:45 pm

StringTokenizer problems
-----------------------------------
I'm trying to load all of the words in a text file into a hashset...but it seems that the first letter is already deleted off automatically for some reason


// The "ArgChecker" class.
import java.io.*;
import java.util.*;

public class ArgChecker
{
    public static void main (String [] args) throws IOException
    {
        Set allwords = new HashSet ();
        String str;
        BufferedReader wordfile = new BufferedReader (new FileReader ("dictionary.txt"));

        StringTokenizer words;

        while (wordfile.read () != -1)
        {
            words = new StringTokenizer (wordfile.readLine ());
            while (words.hasMoreTokens ())
            {

                str = String.valueOf (words.nextToken ());
                System.out.println (str);
                allwords.add (str);
            }
        }
        System.out.println (allwords);

        // Place your code here
    } // main method
} // ArgChecker class


-----------------------------------
McKenzie
Sun Feb 12, 2006 8:07 pm


-----------------------------------
The line:while (wordfile.read () != -1) 

takes the first letter off.
try:
while(line = wordfile.readLine() != null)
        { 
            words = new StringTokenizer (line);
...
 
OR
make a custom method that uses read to read one word at a time.

-----------------------------------
Xi_is4COW
Sun Feb 12, 2006 8:11 pm


-----------------------------------
whoa...i didnt know you noticed the post, well thanks for the help, i'll try that right away.

-----------------------------------
Xi_is4COW
Sun Feb 12, 2006 8:30 pm


-----------------------------------
Err....I tried the 
while(line=wordfile.readLine() != null)
thing but it seems like i dont know how to declare the line variable...

-----------------------------------
Martin
Sun Feb 12, 2006 8:44 pm


-----------------------------------
On the line above that, declare the variable line as a String.

String line;

-----------------------------------
wtd
Sun Feb 12, 2006 8:46 pm


-----------------------------------
Is there some reason the declaration has to be separate?

-----------------------------------
Xi_is4COW
Sun Feb 12, 2006 8:49 pm


-----------------------------------
On the line above that, declare the variable line as a String.

String line;

I tried that, but it just gave me an error that said that the left side "String" was not compatible with the right side "boolean"

-----------------------------------
wtd
Sun Feb 12, 2006 9:02 pm


-----------------------------------
line = wordfile.readLine() != null

Is being read as:

line = (wordfile.readLine() != null)

-----------------------------------
Xi_is4COW
Sun Feb 12, 2006 9:17 pm


-----------------------------------
line = wordfile.readLine() != null

Is being read as:

line = (wordfile.readLine() != null)

So what exactly am I supposed to change in order to make it work?

-----------------------------------
Martin
Sun Feb 12, 2006 10:25 pm


-----------------------------------
The problem is the order of operations.

line = wordfile.readLine() != null

becomes:

line = (wordfile.readLine() != null)

Which evaluates to line = true or line = false.

Think about it like this. If you wanted to evaluate 4+3 * 2 so that it gave you 7 * 2 = 14 (instead of 4 + 6 = 10), you would have to write it as (4+3)*2

So what you want to do is set line to wordfile.readLine(), and then check to see if line is null (just like in the above how we want to add 4 and 3, and then multiply the result by 2). How would you do this using brackets?

-----------------------------------
Xi_is4COW
Sun Feb 12, 2006 10:40 pm


-----------------------------------
Oh I see I see....it was only a misplacement of brackets that was messing me up..
Thanks alot to everyone who helped out :D
