1
 public ArrayList<detailTom>  read() 
{
   try 
   {
       /*System.out.println("Hello");*/
       BufferedReader in = new BufferedReader(new FileReader("G:/sample.txt"));
       String data;
       ArrayList <String> list = new ArrayList<String>();
       ArrayList<String> tomList= new ArrayList<String>();
       ArrayList<detailTom> tomData = new ArrayList<detailTom>();

       while((data= in.readLine())!=null)
       {
           /*System.out.println(data);*/
           String[] splits=data.split(" ");
           for(int i=0;i<splits.length;i++)
           {
                 if (splits[i].equals("tom"))
                 {
                       detailTom dummy= new detailTom();
                        dummy.name="tom";
                        dummy.mark1=Integer.parseInt(splits[1]);
                        dummy.mark2=Integer.parseInt(splits[2]);
                        dummy.mark3=Integer.parseInt(splits[3]);
                        dummy.mark4=Integer.parseInt(splits[4]);
                        dummy.mark5=Integer.parseInt(splits[5]); 

                        tomData.add(dummy);

                 }
           }

       }

   }

   catch (FileNotFoundException ex) 
   {
       Logger.getLogger(MyInterface.class.getName()).log(Level.SEVERE, null, ex);
   } catch (IOException ex) 
   {
       Logger.getLogger(MyInterface.class.getName()).log(Level.SEVERE, null, ex);
   }
   return tomData;


} 

Hello!

I am trying to return a variable tomData which is an object of class ArrayList. detailTom is a class which has variables , getters , setters and so forth . What I want to do is my method read() to return tomData. But I get an error at the line return tomData.

The error says:-

cannot find symbol Symbol: variable tomData

4 Answers 4

5

You've declared tomData inside your try-block, so it isn't visible outside. Try moving the declaration before the try-block.

ArrayList<DetailTom> tomData;

try {
    ...
    tomData = ...

Also note that I've assumed a more standard naming convention: class names should start with a capital letter.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your comment - But i initialized tomData outside the try block. That worked!!
You'll need to initialise tomData to something for this to work - otherwise it won't compile as tomData may not have been initialised.
4

tomData is out of scope when you're trying to return it. There's a number of ways around this, but the easiest would be to declare and initialise tomData before the try block.

Comments

3

tomData is declared inside try block.Variables declared inside try block are not accisible outside of it.So just declare tomData out of try block.Try this

public ArrayList<detailTom>  read() 
{
   ArrayList<detailTom> tomData = null;
   try 
   {
       /*System.out.println("Hello");*/
       BufferedReader in = new BufferedReader(new FileReader("G:/sample.txt"));
       String data;
       ArrayList <String> list = new ArrayList<String>();
       ArrayList<String> tomList= new ArrayList<String>();
       tomData  = new ArrayList<detailTom>();

       while((data= in.readLine())!=null)
       {
           /*System.out.println(data);*/
           String[] splits=data.split(" ");
           for(int i=0;i<splits.length;i++)
           {
                 if (splits[i].equals("tom"))
                 {
                       detailTom dummy= new detailTom();
                        dummy.name="tom";
                        dummy.mark1=Integer.parseInt(splits[1]);
                        dummy.mark2=Integer.parseInt(splits[2]);
                        dummy.mark3=Integer.parseInt(splits[3]);
                        dummy.mark4=Integer.parseInt(splits[4]);
                        dummy.mark5=Integer.parseInt(splits[5]); 

                        tomData.add(dummy);

                 }
           }

       }

   }

   catch (FileNotFoundException ex) 
   {
       Logger.getLogger(MyInterface.class.getName()).log(Level.SEVERE, null, ex);
   } catch (IOException ex) 
   {
       Logger.getLogger(MyInterface.class.getName()).log(Level.SEVERE, null, ex);
   }
   return tomData;


} 

Comments

3

Your return tomData at the bottom occurs outside of the try block where tomData was declared, so it is out of scope, try declare tomData at the top of the method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.