How to return a list?
Hello,
I'm just trying to return the value of a list after it has been read from a file, but for somereason i don't get it. Can someone help me spot the error?
publicstatic ArrayList getArrayList(Object[] a){
List assList =new ArrayList();
try{
FileReader fin =new FileReader(fileName);
BufferedReader in =new BufferedReader(fin);
int i = 0;
String line = in.readLine();
while (line !=null){
StringTokenizer st =new StringTokenizer(line);
while (st.hasMoreTokens()){
String currWord = st.nextToken();
assList.add(i, currWord);
i++;
}
line = in.readLine();
}
Collections.sort(assList);
}catch(FileNotFoundException f){
System.out.println("File not found:");
}catch (Exception e){
System.out.println("General Exception Caught");
}
return asslist;
}
[1746 byte] By [
Vmastaa] at [2007-10-2 20:31:11]

Hi,you can not return a List instead of ArrayList.declare 'assList' as an ArrayList.Regards,Saptagiri
Is there a specific reason code calling this method needs to know it will receive an ArrayList? If not, change the return type of the method to just plain List.
As as aside, what does the list contain? Should the method really be called getArrayList()? Wouldn't a descriptive name like getWords() be more appropriate?
I would recommend something more like this.
public static List getWords(String filename)
throws Exception
{
try {
FileReader fin = new FileReader(filename);
BufferedReader in = new BufferedReader(fin);
List words = new ArrayList();
String line;
while((line = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
words.add(st.nextToken());
}
}
Collections.sort(words);
return words;
} catch (FileNotFoundException f) {
throw new Exception("File not found: " + filename);
} catch (IOException ioe) {
throw new Exception("Failed to read the file: " + filename);
}
}
If the method is going to be public and static then it shouldn't rely upon the state of class variables (fileName) unless they are constants (in which case the variable name should be in ALL CAPITALS).
It should not take parameters that are not used in the method body.
It should return List
It should have a meaningful name
It probably shouldn't return an empty list if it encounters a problem.
I do not like the following code at all:
try {
SOMETHING;
} catch (FileNotFoundException f) {
throw new Exception("File not found: " + filename);
} catch (IOException ioe) {
throw new Exception("Failed to read the file: " + filename);
}
It just replaces a more specific exception by a general one.
It looses information.
It's worse then simply SOMETHING by any criterion.
But you're right with the your objection. Thing like
System.out.println("File not found");
should trigger a compile time error, using something like
e.printStackTrace();
is much better.
In this way you can get the list. Code is given below.
import java.util.*;
import java.io.*;
class fileal
{
public static void main(String args[])
{
ArrayList al1=getArrayList("aa.txt");
}
public static ArrayList getArrayList(String fileName)
{
ArrayList assList = new ArrayList();
try{
FileReader fin = new FileReader(fileName);
BufferedReader in = new BufferedReader(fin);
int i = 0;
String line = in.readLine();
while (line != null) {
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
String currWord = st.nextToken();
assList.add(i, currWord);
i++;
}
line = in.readLine();
}
Collections.sort(assList);
ListIterator lt=assList.listIterator();
while(lt.hasNext())
System.out.println(lt.next());
}catch(FileNotFoundException f){
System.out.println("File not found:");
}catch (Exception e){
System.out.println("General Exception Caught");
}
return assList;
}
}
yeah, i dont understand why youre working with index...when you add an object to an ArrayList, it adds that object to the next open node in the list, so dont worry about index.