Computer Science Canada

StringTokenizer problems

Author:  Xi_is4COW [ Sun Feb 12, 2006 3:45 pm ]
Post subject:  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

code:

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

Author:  McKenzie [ Sun Feb 12, 2006 8:07 pm ]
Post subject: 

The line:
code:
while (wordfile.read () != -1)

takes the first letter off.
try:
code:
while(line = wordfile.readLine() != null)
        {
            words = new StringTokenizer (line);
...

OR
make a custom method that uses read to read one word at a time.

Author:  Xi_is4COW [ Sun Feb 12, 2006 8:11 pm ]
Post subject: 

whoa...i didnt know you noticed the post, well thanks for the help, i'll try that right away.

Author:  Xi_is4COW [ Sun Feb 12, 2006 8:30 pm ]
Post subject: 

Err....I tried the
code:
while(line=wordfile.readLine() != null)

thing but it seems like i dont know how to declare the line variable...

Author:  Martin [ Sun Feb 12, 2006 8:44 pm ]
Post subject: 

On the line above that, declare the variable line as a String.

Java:
String line;

Author:  wtd [ Sun Feb 12, 2006 8:46 pm ]
Post subject: 

Is there some reason the declaration has to be separate?

Author:  Xi_is4COW [ Sun Feb 12, 2006 8:49 pm ]
Post subject: 

Martin wrote:
On the line above that, declare the variable line as a String.

Java:
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"

Author:  wtd [ Sun Feb 12, 2006 9:02 pm ]
Post subject: 

code:
line = wordfile.readLine() != null


Is being read as:

code:
line = (wordfile.readLine() != null)

Author:  Xi_is4COW [ Sun Feb 12, 2006 9:17 pm ]
Post subject: 

wtd wrote:
code:
line = wordfile.readLine() != null


Is being read as:

code:
line = (wordfile.readLine() != null)


So what exactly am I supposed to change in order to make it work?

Author:  Martin [ Sun Feb 12, 2006 10:25 pm ]
Post subject: 

The problem is the order of operations.

code:
line = wordfile.readLine() != null


becomes:

code:
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?

Author:  Xi_is4COW [ Sun Feb 12, 2006 10:40 pm ]
Post subject: 

Oh I see I see....it was only a misplacement of brackets that was messing me up..
Thanks alot to everyone who helped out Very Happy


: