tags:

views:

87

answers:

3

Hello, I am new at java. I am doing the following:

Read from file, then put data into a variable.

I have declared the checkToken and lineToken as public strings under the class.

    public static void readFile(String fromFile) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(fromFile));
          String line = null;
          while ((line=reader.readLine()) != null )  {
          if (line.length() >= 2) { 
               StringTokenizer lineToken = new StringTokenizer (line);
               checkToken = lineToken.nextToken();
               processlinetoken()
               ......

But here's where I come into a problem.

     public static void processlinetoken()
          checkToken=lineToken.nextToken();
     }

it fails out.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 The method nextToken() is undefined for the type String

 at testread.getEngineLoad(testread.java:243)
 at testread.readFile(testread.java:149)
 at testread.main(testread.java:119)

so how do I get this to work? It seems to pass the variable, but nothing after the . works.

The Eclipse IDE is not much help "Link all references for a local rename (does not change references in other files)" Rename in file is the only option. It does not do anything.

+2  A: 

It just seems that lineToken is a String instead that a StringTokenizer. Probably you've declared String lineToken as an attribute of your class and you planned to use it aroung but in the readFile method you assign to lineToken a new StringTokenizer() but your definition is local and shadows the one you are doing out.

You should try by removing StringTokenizer from

StringTokenizer lineToken = new StringTokenizer (line);

and just do

lineToken = new StringTokenizer(line);
Jack
Thank you. This did it. I was declaring public String. Instead of public StringTokenizer. I didn't know there was a difference. I thought StringTokenizer was an extension of String. public static StringTokenizer lineToken; public static String checkToken;
Adam Outler
A: 

You're trying to call nextToken() on a String on line 243 of testread.java. If you're using an IDE, it should show you the problem.

Kaleb Brasee
it says " The method nextToken() is undefined for the type String"It does not say this in the readFile method, but then when I go to the processlinetoken() it does not pass. this is my question. Why does it give me this message?
Adam Outler
It says "Link all references for a local rename (does not change references in other files)" then it gives me the option to rename.but it does not work.
Adam Outler
A: 

Thank you Jack.

I was declaring a public static String, but then a StringTokenizer in the readFile(). So when calling the ProcessLineToken(), it was looking at the null String variable, not the StringTokenizer which was declared in readFile().

I misunderstood the use of StringTokenizer. It's a type, not an extension.

Adam Outler