The Artima Developer Community
Sponsored Link

Java Answers Forum
Cannot get program to properly count tokens

1 reply on 1 page. Most recent reply: Oct 31, 2003 8:34 AM by Joe Parks

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Jenna

Posts: 1
Nickname: jennam
Registered: Oct, 2003

Cannot get program to properly count tokens Posted: Oct 31, 2003 7:53 AM
Reply to this message Reply
Advertisement
I'm sure its something simple, but I cannot seem to figure out the syntax error in my program. It's supposed to count the tokens read from a file and print the total number counted at the end of a new file. However, I keep getting a "counter variable may not have been initialized".

Code

import java.io.*; //import i/o for standard keyboard input output
import java.util.*;//import util for StringTokenizer

public class XYZ{
public static void main (String[] args) throws FileNotFoundException, IOException
{
String inputLine;//variable that the data read from file will be stored as
StringTokenizer tokenizer;//needed to tokenize string

int Counter;//will store the number of tokens counted

BufferedReader inFile
= new BufferedReader(new FileReader("i:\\instruction.txt"));//where the file is to be read from

PrintWriter outFile
= new PrintWriter(new BufferedWriter(new FileWriter("i:\\amended_Instr.txt")));//where the new file
//will be outputted to

while ((inputLine = inFile.readLine()) != null)//use a while loop to modify our inputLine
//each token at a time
{
inputLine = inputLine.trim();//trims the white space off the beginning and end of code
inputLine = inputLine.replace(',', ' '); //for the sake of StringTokenizer
//because ',' is not a default
//delimiter for it
tokenizer = new StringTokenizer(inputLine);
Counter += tokenizer.countTokens();
outFile.println(inputLine + "Number of tokens: " + Counter);//displays results in Amended_Instr.txt
}//end while loop
outFile.close();
}
}


Error

XYZ.java:27: variable Counter might not have been initialized
Counter += tokenizer.countTokens();
^
XYZ.java:29: variable Counter might not have been initialized
outFile.println(inputLine + "Number of tokens: " + Counter);//displays results in Amended_Instr.txt
^
2 errors


Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Cannot get program to properly count tokens Posted: Oct 31, 2003 8:34 AM
Reply to this message Reply
Instance variables are implicitly initialized to their default values (e.g., 0 for ints, null for objects). Local variables are not. They must be explicitly initialized before use.
int Counter = 0;

Flat View: This topic has 1 reply on 1 page
Topic: Free Lightweight DB with Stored Procedures? Previous Topic   Next Topic Topic: Exception while creating instance of EJB

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use