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]

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;
}
}
}
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!