Why won't StringTokenizer tokenize! (Java)

Status
Not open for further replies.

Spatula

Ars Tribunus Angusticlavius
9,315
Subscriptor
code:<HR>
Code:
package Assignment1;<P>/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:
 * @author
 * @version 1.0
 */
import java.io.*;
import java.util.*;
public class Assignment1 {<P>  public Assignment1() {<P>  try {
    //open the text file
    BufferedReader DataIn = new BufferedReader(new FileReader("data.txt"));<P>
    //now find the number of lines
    int numberOfLines = 0;
    String inputLine = DataIn.readLine();
    while (inputLine != null) {
    numberOfLines++;
  }
    DataIn.close();<P>    //now make the arrays
    BufferedReader DataIn2 = new BufferedReader(new FileReader("FileName"));
    String[][] a = new String[numberOfLines][];
    StringTokenizer line = DataIn2.readLine() ;<P>    for(int x=0; x>=numberOfLines; x++)
        while (line != null) {
        int wordCounter = 0;
        while (line.hasMoreTokens()) {
        line.nextToken();
        wordCounter++;
        a[x] = new String[wordCounter];
        }
      }
    DataIn2.close();<P>    //now fill them in
    BufferedReader DataIn3 = new BufferedReader(new FileReader("FileName"));
    StringTokenizer lineForFilling = null;
    while (lineForFilling != null) {<P>    //text
  }<P>
}<P>
    //some exceptions relating to file i/o
    catch( FileNotFoundException e )
    {
      System.out.println("file data.txt not found or could not be opened") ;
    }
    catch( IOException e )
    {
      System.out.println( "Error reading from file data.txt" );
    }<P>}//close try
}//close Assignment 1

The error I get in JBuilder is
code:<HR>
Code:
"Assignment1.java": Error #: 354 : incompatible types; found: java.lang.String, required: java.util.StringTokenizer at line 33, column 44

I know this program is a mess, honestly, I'm not a programmer, I'm an EE at a school where they tie the two together very closely. So I have no interest in suggestions on how to improve it (although I thank anyone who would have made suggestions along those lines), as it would be acedemic dishonesty.

Thanks
Noel

PS- right now it doesn't do anything, just runs.
 
Edit: Wait, why are you asking for help on it if it would be academic
dishonesty? Lock the thread and ask your teacher or a TA or something.


Originally posted by noela:

String inputLine = DataIn.readLine();
while (inputLine != null) {
numberOfLines++;
}

You're not doing anything in that while loop to get out of it. How
about
code:<HR>
Code:
while ((inputLine = DataIn.readLine()) != null) {
 numberOfLines++;
}

~Kevin
:)
 

Spatula

Ars Tribunus Angusticlavius
9,315
Subscriptor
SSShhhh..... like I said, this program is a peice of shamble, I forgot to tell it to read the next line. :)

I'm mostly concerned about the Tokenizer thing...

code:<HR>
Code:
//now find the number of lines
    int numberOfLines = 0;
    String inputLine = DataIn1.readLine();
    while (inputLine != null) {
    inputLine = DataIn1.readLine();
    numberOfLines++;
  }

This is what I meant it to be, which is how I would have actually written it (although your method is nice and pretty)
 

R2P2

Ars Praefectus
3,783
Subscriptor
The problem is that this:
code:<HR>
Code:
StringTokenizer line = DataIn2.readLine();
should be this:
code:<HR>
Code:
StringTokenizer line = new StringTokenizer(DataIn2.readLine());
You need to pass the string to the StringTokenizer constructor to get a StringTokenizer for it, whereas the code you posted is trying to tell Java that the String *is* a StringTokenizer.
 

Spatula

Ars Tribunus Angusticlavius
9,315
Subscriptor
Nonono, this would be academic dishonesty if I was looking for suggestions on how to do the project. I'm just trying to figure out why this error is being thrown by JBuilder. Our prof/TAs don't care if we get help with problems we can't solve, it's when we get together to solve an assignment from scratch without trying it ourselves that's a no go.

Like I said originally, I'm NOT interested in "You can do this better by...", I'm interested in "God damn you StringTokenizer, you work in the example code the prof gave, but for some reason you don't work here"

We were given the example of how to use StringTokenizer, but are not expected to know how to use it really (if that makes sense). The prof just didn't want us to try re-inventing the wheel (before he gave us StringTokenizer, I had half-written a program to do the same thing as StringTokenizer, using solely the detection of white spaces in Strings... ugh)
 

Spatula

Ars Tribunus Angusticlavius
9,315
Subscriptor
Ah, you're right! Thank you so much.

I checked in the example StringTokenizer the prof gave us, and he did this:

code:<HR>
Code:
StringTokenizer wordLine = null ;

Then, in a while loop below he did:

code:<HR>
Code:
wordLine = new StringTokenizer( line ) ;

I had missed the second snippet of code that passed it to the constructor.

Thanks!
 

wb

Ars Legatus Legionis
25,208
Subscriptor
StringTokenizer is crap anyway. It doesn't tokenize, it just splits; there's a difference -- if your tokens have embedded separators (consider, a space-delimited sequence of tokens, where tokens can have backslash-escaped internal spaces), it can't cope.

Why not do something like:

code:<HR>
Code:
StringTokenizer toks = new StringTokenizer(expr,delim+escChar,true);
while( toks.hasMoreElements() )
{
   String tok = toks.nextToken(delims+escChar);
   if( tok.equals( escChar ) )
   {
      tok = toks.nextToken(escChar);
   }
   else if( tok.equals(delim) )
   {
      tok = toks.nextToken(delims+escChar);
   }
// do whatever with token
}

Although I guess the code above would fail if you had an escape character next to a delimiter, but that wouldn't be too hard to fix.
 
Status
Not open for further replies.