i have created a program below and i am having a problem with a variable called numWords i am trying to call this variable from near the bottom of of my code but i keep gettin the following error:
H:\qp&e\mess\halstead.java:1178: variable numWords might not have been initialized N_2 = numWords - N_1; ^ 1 error Finished
i dont know what i'm doing wrong. numWords has been declared near the top of my coding. numWords counts the total number of words in a text file and i want to subtract N_1(total operaters counted) from numWords but i keep getting a error. N_1 is declared near the bottom of my code.
below is my code, its very long:
[ EJFH: Added UBB "Code" tags for formatting ] [ December 08, 2004: Message edited by: Ernest Friedman-Hill ]
There are three or four different variables named numWords in your program; each one is local to one method or another. Most of them are initialized, but the offending one -- the one declared near the end of main(), is not. So you've got
numWords is not assigned to anywhere before you read its value in the last line above; therefore, Java won't let you do it. Because it's never been assigned to, its value is undefined.
Not sure if you think all the methods are sharing a single numWords variable, or not -- but they're not. You could make numWords into a member variable if you wanted them all to share a single variable.
Now, let me give you some advice you didn't ask for. The vast, vast majority of this program is taken up by a long list of String declarations, followed by a long chain of "if" statements, all alike, that compare a String against one of those variables and then increment a variable if they match. Such a long, long swath of code makes a program impossible to manage -- if you can't fit a method on a single screen, then you can never see the whole routine, and this makes it very hard to understand. Here's how I would rewrite all that code to get it down from hundreds of lines to just a few:
That's it. No dozens of "if" statements -- just that one.
Joined: Nov 30, 2004
thank you for your help. i tried your code and got one error and i was hoping you could tell me where i have gone wrong.
No, you've put it in then right place. I'd read what the errors say and try to fix those problems, because the missing bracket was not the only thing wrong with your code. You could keep posting every compiler problem you encounter in this forum, but that going to be a very slow way to write programs.