Java – Text File and StringTokenizer – cannot find symbol?

Java – Text File and StringTokenizer – cannot find symbol?
I am writing a simple code to read a text file and split the lines of string into tokens… it is giving error “cannot find symbol” for String Tokenizer. Also, if someone could let me know if I am reading the file correctly and storing the string tokens correctly. :) This is the error:

Employer.java:18: cannot find symbol
symbol : constructor StringTokenizer(java.util.Scanner,java.lang.String)
location: class java.util.StringTokenizer
StringTokenizer st = new StringTokenizer(inputFile, ” “);

This is my code:

import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;

public class Employer
{
public static void main (String[] args)
{

File Information = new File(“Information.txt”);
Scanner inputFile = new Scanner(Information);

StringTokenizer st = new StringTokenizer(inputFile, ” “);

while (inputFile.hasNext())
{

String firstName = st.nextToken();
String lastName = st.nextToken();
String employeeNum = st.nextToken();
String hireDate = st.nextToken();
String shiftNum = st.nextToken();
String payRate = st.nextToken();

}

inputFile.close();

}
}

Best answer:

The StringTokenizer class has 3 constructors. Yours doesn’t appear to be any of them.

see

java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html

Tags: , , , , , ,

Under Forum

1 Comment for Java – Text File and StringTokenizer – cannot find symbol?

  • 1. David Karr  |  May 9th, 2006 at 12:56 am

    Store the first source link as a bookmark/favorite and refer to it often (or even better, download the javadocs and bookmark the local version).

    Go to the second source link and look in the “Constructor Summary” section. Note the choices for parameters to the constructor.

    Now look at the line that is failing to compile and note what parameters you’re trying to send. The second StringTokenizer constructor takes a “String str” and “String delim”. Your first parameter is a “Scanner”, not a “String”.

    You need to move the StringTokenizer construction inside the loop, and store each line you read from the scanner into a string, and then create the StringTokenizer from that string, not from the scanner.

Leave a Comment for Java – Text File and StringTokenizer – cannot find symbol?

You must be logged in to post a comment.

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories