Author |
Message |
Xi_is4COW
|
Posted: 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
|
Description: |
|
Download |
Filename: |
dictionary.txt |
Filesize: |
194 Bytes |
Downloaded: |
86 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
McKenzie
|
Posted: Sun Feb 12, 2006 8:07 pm Post subject: (No 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.
|
|
|
|
|
|
Xi_is4COW
|
Posted: Sun Feb 12, 2006 8:11 pm Post subject: (No subject) |
|
|
whoa...i didnt know you noticed the post, well thanks for the help, i'll try that right away.
|
|
|
|
|
|
Xi_is4COW
|
Posted: Sun Feb 12, 2006 8:30 pm Post subject: (No 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...
|
|
|
|
|
|
Martin
|
Posted: Sun Feb 12, 2006 8:44 pm Post subject: (No subject) |
|
|
On the line above that, declare the variable line as a String.
|
|
|
|
|
|
wtd
|
Posted: Sun Feb 12, 2006 8:46 pm Post subject: (No subject) |
|
|
Is there some reason the declaration has to be separate?
|
|
|
|
|
|
Xi_is4COW
|
Posted: Sun Feb 12, 2006 8:49 pm Post subject: (No subject) |
|
|
Martin wrote: On the line above that, declare the variable line as a String.
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
|
Posted: Sun Feb 12, 2006 9:02 pm Post subject: (No subject) |
|
|
code: | line = wordfile.readLine() != null |
Is being read as:
code: | line = (wordfile.readLine() != null) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Xi_is4COW
|
Posted: Sun Feb 12, 2006 9:17 pm Post subject: (No 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?
|
|
|
|
|
|
Martin
|
Posted: Sun Feb 12, 2006 10:25 pm Post subject: (No 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?
|
|
|
|
|
|
Xi_is4COW
|
Posted: Sun Feb 12, 2006 10:40 pm Post subject: (No 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
|
|
|
|
|
|
|