views:

91

answers:

3

Hello.


I need help. For this specific method. I am trying to get it to return an arraylist that I tokenized.

public ArrayList read (){

  BufferedReader inputStream = null;
  try {
    inputStream = new BufferedReader(new FileReader("processes1.txt"));
    String l;
    while ((l = inputStream.readLine()) != null) {

      ArrayList<String> tokens = new ArrayList<String>();

      Scanner tokenize = new Scanner(l);
      while (tokenize.hasNext()) {
        tokens.add(tokenize.next());
      }
      return tokens;
    }
  } catch(IOException ioe){
    ArrayList<String> nothing = new ArrayList<String>();
    nothing.add("error1");
    System.out.println("error");
    //return nothing;
  }
  return tokens;
}

What am I doing wrong?!

+4  A: 

At the very end you are doing return tokens but that variable was defined INSIDE the try block, so it is not accessible outside of it. You should add:

ArrayList<String> tokens = new ArrayList<String>();

to the top of your method, just under the BufferedReader.

webdestroya
oh thank you. but now am i having this error once it compiles
Luron
Exception in thread "main" java.lang.NoSuchMethodError: mainPress any key to continue . . .
Luron
@Luron - That's an entirely different problem, that means you didn't specify a `public static void main(String[] args)` method inside your class. This is required by the JVM
webdestroya
yeh i know. thats why i said i got it.
Luron
A: 

Try returning ArrayList which is the more appropriate return type in this case. Generic types aren't related to each other the way your example seems to be using them.

Aurojit Panda
A: 

It is probably an error in your main method somewhere. Are you instantiating the class and calling the method read() on it?

venky
yeh i figured it out thank you.
Luron