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