Returning a value in a Method

I need help with returning values in methods. To give you the basics I'm trying to write a lines of code counter. Currently my code compiles and runs but it doesn't really do anything becuase none of the values in the method get's returned to the main (If you run it you have to manually stop it). Currently I'm just trying to work on filename becuase I figure if I can get it working then the rest will be similiar. Can someone help me figure out how to return filename to my main from inputFile()?

Here is my code:

import java.util.*;

import java.io.*;

import java.text.*;

import java.lang.*;

publicclass Homework2b

{

publicstaticvoid main (String[] args)

{

String name="";

String filename="";

String line="";

int count=0, flag=0;

while(!"stop".equals(filename))

{

inputFile();

System.out.println("Return filename is: "+filename);

loadFile(filename);

countLines(line);

displayLOCS(filename, count, flag);

}

}

publicstatic String inputFile()

{ String filename;

System.out.println("Enter source code filename or stop to end: ");

Scanner scan=new Scanner (System.in);

filename=scan.nextLine();

System.out.println("The filename that should be made is: " +filename);

return filename;

}

publicstatic String loadFile(String filename)

{

String line="";

int flag=0;

try

{

BufferedReader file=new BufferedReader(new FileReader(filename));

line=file.readLine();

}

catch (IOException e)

{

System.out.println("I/O Error occured while trying to access "+filename);

flag=1;

}

catch (NoSuchElementException e)

{

System.out.println("I/O Error occured while trying to access "+filename);

flag=1;

}

return line;

}

publicstaticint countLines(String line)

{

int count=0;//String theline=line;

while(line!=null)

{

count++;

}

return count;

}

publicstaticvoid displayLOCS(String filename,int count,int flag)

{

if (flag!=1)

{

System.out.println(filename+" has "+ count+" lines of code.");

System.out.println();

count=0;

}

else

{

System.out.println();

flag=0;

count=0;

}

}

}

[4723 byte] By [davegelha] at [2007-11-26 18:03:31]
# 1
The values get returned to main, but you ignore them.For instance, in main wher you call countLines, that method is returning an int, but you just ignore it. You need something likeint numLines = countLines(...);
jverda at 2007-7-9 5:33:40 > top of Java-index,Java Essentials,New To Java...
# 2
dave,public static int countLines(String line) //says this this returns and integer, yes?/*but you call it with*/ countLines(line); //meaning that you throw away the return value.Oops!keith.
corlettka at 2007-7-9 5:33:40 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks jverd yours work. Now I'm just getting trapped in the loop at countLine(). I know what's causing the problem becuase I programmed it to keep updating line in countLine but didn't program the inputFile() to keep updateing. Anyone know of a way to do this.
davegelha at 2007-7-9 5:33:40 > top of Java-index,Java Essentials,New To Java...
# 4
dave, apply the same "pattern as above".... you're chucking your own return values again ya' dunce. keith.
corlettka at 2007-7-9 5:33:40 > top of Java-index,Java Essentials,New To Java...
# 5

corlettk I don't appreciate being called a dunce. Here is the code that is with the other guys fix and it works.

import java.util.*;

import java.io.*;

import java.text.*;

import java.lang.*;

public class Homework2b

{

public static void main (String[] args)

{

String name="";

String filename="";

String line="";

int count=0, flag=0;

while(!"stop".equals(filename))

{

filename=inputFile();

System.out.println("Return filename is: "+filename);

line=loadFile(filename);

count=countLines(line);

displayLOCS(filename, count, flag);

}

}

public static String inputFile()

{ String filename;

System.out.println("Enter source code filename or stop to end: ");

Scanner scan= new Scanner (System.in);

filename=scan.nextLine();

System.out.println("The filename that should be made is: " +filename);

return filename;

}

public static String loadFile(String filename)

{

String line="";

int flag=0;

try

{

BufferedReader file= new BufferedReader(new FileReader(filename));

line=file.readLine();

}

catch (IOException e)

{

System.out.println("I/O Error occured while trying to access "+filename);

flag=1;

}

catch (NoSuchElementException e)

{

System.out.println("I/O Error occured while trying to access "+filename);

flag=1;

}

System.out.println("The first line is: "+line);

return line;

}

public static int countLines(String line)

{

int count=0;//String theline=line;

while(line!=null)

{

count++;

System.out.println(count);

}

return count;

}

public static void displayLOCS(String filename, int count, int flag)

{

if (flag!=1)

{

System.out.println(filename+" has "+ count+" lines of code.");

System.out.println();

count=0;

}

else

{

System.out.println();

flag=0;

count=0;

}

}

}

davegelha at 2007-7-9 5:33:40 > top of Java-index,Java Essentials,New To Java...
# 6
> corlettk I don't appreciate being called a dunce.Well, if you're shown your mistake, and then you make the same mistake again with the same results, and you don't recognize it as the same, maybe you've earned the pointy hat. :-)
jverda at 2007-7-9 5:33:40 > top of Java-index,Java Essentials,New To Java...
# 7

dave,

You wheren't really intended to like being called a dunce, but I really didn't expect you take serious umbrage, and I really really didn't intend to offend you, so I apoligise... but if you intend to stay in this game you then are gonna need a thicker hide.

You are gonna make mistakes, and the people who paid you to stuff it all up are gonna call you all the names under the sun, and threaten you with all kinds of fiscal rectitudes... the trick is to just stand there, keep your mouth shut, until they've finish ranting.... then ask them "What are we going to do about it". Getting offended really won't help.

Glad you got it working.

keith.

Message was edited by: corlettk - I can't type!

corlettka at 2007-7-9 5:33:40 > top of Java-index,Java Essentials,New To Java...
# 8

I can take constructive critism, however that to me seemed like you were just calling me a name for no reason but from you recent post I think what happened is that you didn't understand my second post and thought I was doing the same thing, which I wasn't. I had already changed it so that all the stuff ran. However I get caught in my while loop becuase I only import the first line and never import the second line.I was trying to ask if you knew of a way of fixing that.

davegelha at 2007-7-9 5:33:40 > top of Java-index,Java Essentials,New To Java...