By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Help with line count

P: 10
Hi, I am having problems with this bit of code:


public class main
{


public class readAccounts
{
reader1 r = new reader1();

}

void readAccounts()
{
printFile("account.txt");
}
static void printFile(String fileName)
{
System.out.println();

String[] lines = LineIO.readAllLines(fileName);
for (String l : lines)

{
System.out.println(l);
}
}
public static void main(String[] args)
{
for (String arg : args)
{
printFile(arg);
}
}
// print number of lines


private static void count(String name)
{
long numLines = 0;
String lines;
do {
lines = LineIO.readAllLines(fileName);
if (lines != null)
{
numLines++;


}
}
while (line != null);
System.out.println(numLines)

}}

The basic function is to read a text document then display the document with the amount of lines in the text file. So far it works fine if the print lines code is taken out. It will display the text document perfectly. However when the new code is added I get an error message saying that 'cannot find symbol = variable file name'

Im sure this is simple to rectify and any help would be welcomed

Thankyou
Dec 9 '06 #1
Share this Question
Share on Google+
3 Replies


DeMan
100+
P: 1,806
in your count method, you pass in a String called name, and then try to readAll fileName, you might like to change one of them.

What is the while loop doing down the bottom? (it loops while line!=null BUT I don't think it can see line (in which case it won't run), nor is line being modified (so it will run forever), and finally it has a semi-colon behind it (so it won't do aything). Are you trying to run multiple threads or something (or did this accidently slip in).....?
Dec 10 '06 #2

DeMan
100+
P: 1,806
Sorry, the do...while structure caught me out - nonetheless I think it should be lines not line......(you can ignore the rest of what I said...I wasn't looking properly.
Dec 10 '06 #3

10K+
P: 13,264
Sorry, the do...while structure caught me out - nonetheless I think it should be lines not line......(you can ignore the rest of what I said...I wasn't looking properly.
When posting code please use code tags so that your code is readable.

This method will not compile because you are using the variable fileName which is neither declared nor passed into the method.

Expand|Select|Wrap|Line Numbers
  1.  private static void count(String name) { 
  2.  long numLines = 0;
  3.  String lines;
  4.  do {
  5.    lines = LineIO.readAllLines(fileName);  //fileName is unknown here
  6.    if (lines != null) {
  7.      numLines++;
  8.    }
  9.  }
  10.  while (line != null);
  11.  System.out.println(numLines)
  12. }
  13.  
Dec 11 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.