• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

variable problem

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to move this over to Java in General (Beginner), because this is far from an advanced question -- and then I'll answer it.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, here we are in JiG(B).

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

long numWords;
...
N_1 = count;
...
N_2 = numWords - N_1;

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.
 
sukh singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

i get the following error:-

H:\qp&e\halstead.java:45: ')' expected
if (Arrays.binarySearch(words), currToken) > -1)
^
1 error
Finished


here is the code so far:-

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

class halstead{
//for keyboard input
static BufferedReader keyboard = new BufferedReader(new
InputStreamReader(System.in));
//for screen output
static PrintWriter screen = new PrintWriter(System.out, true);
//main method - first method to be executed
public static void main(String[]args)throws IOException{
String line = null;
String file;

String[] words = { "ADDHANDLER", "ADDRESSOF", "ANDALSO", "ALIAS", "AND" };

//use screen object (class PrintWriter) to display
//string on screen
screen.print("enter the name of the file to be read: ");
screen.flush();
file = new String(keyboard.readLine());

//a bufferedreader object that takes a stream of characters
//from the fileraeader object and sends a string into
//the program
BufferedReader reader = new BufferedReader(new FileReader(file));

//the bufferedreader method readline() reads from the
//character stream to program a line of characters
line = reader.readLine();

//while loop where readline method returns a null value when
//the end of the file is reached
while (line!= null)
{

//decleration of string tokenizer
//used to seperate each word for comparison to terms specified
StringTokenizer words = new StringTokenizer(line);


Arrays.sort(words);
String currToken = words.nextToken();
if (Arrays.binarySearch(words), currToken) > -1)
++count;


line = reader.readLine();
}

}
}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you (and Ernest ) have done wrong is missed out a bracket. Have a look at the line:

and you should notice what's missing.
 
sukh singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your help, i have put in the missing bracket but i might have put it in the wrong place because i now have 4 errors.

if (Arrays.binarySearch((words), currToken) > -1)


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

class halstead{
//for keyboard input
static BufferedReader keyboard = new BufferedReader(new
InputStreamReader(System.in));
//for screen output
static PrintWriter screen = new PrintWriter(System.out, true);
//main method - first method to be executed
public static void main(String[]args)throws IOException{
String line = null;
String file;

String[] words = { "ADDHANDLER", "ADDRESSOF", "ANDALSO", "ALIAS", "AND" };

//use screen object (class PrintWriter) to display
//string on screen
screen.print("enter the name of the file to be read: ");
screen.flush();
file = new String(keyboard.readLine());

//a bufferedreader object that takes a stream of characters
//from the fileraeader object and sends a string into
//the program
BufferedReader reader = new BufferedReader(new FileReader(file));

//the bufferedreader method readline() reads from the
//character stream to program a line of characters
line = reader.readLine();

//while loop where readline method returns a null value when
//the end of the file is reached
while (line!= null)
{

//decleration of string tokenizer
//used to seperate each word for comparison to terms specified
StringTokenizer words = new StringTokenizer(line);


Arrays.sort(words);
String currToken = words.nextToken();
if (Arrays.binarySearch((words), currToken) > -1)
++count;


line = reader.readLine();
}

}
}
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic