2

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.

4
  • Can you post more of your code? It seems like the lineToken that the method processlinetoken() is calling is not a StringTokenizer but a String. But it's hard to see. Commented Jun 17, 2010 at 1:20
  • Splitting up your code snippets and posting pieces out of order as you've done is pretty confusing. It'd be better if you edit your question to show just the two methods concerned, in their entirety. Commented Jun 17, 2010 at 1:20
  • I reorganized to make it more apparent. Commented Jun 17, 2010 at 1:36
  • Hopefully someone else will explain in more detail, but basically you're not passing anything to the 'processToken' method; you're going to need to pass something to it (the token), then get the return value from it (as the next token) and do whatever else. Delete the static variables you have called 'lineToken' and 'checkToken'. Commented Jun 17, 2010 at 1:42

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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;
0

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.

2 Comments

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?
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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.