Results 1 to 4 of 4
  1. #1
    lemontree45 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default use array from one class in another class.

    Hi,

    Please help me understand, how to use an array from one class in another.

    May be I have missed some lines from the code in the first class, but the second class is the one where I create the array.( I have a .txt file which I have mentioned as this.myFile. The code is long since I have implemented using the Swing in Netbeans.)

    First class:The one with the main function.
    Java Code:
    public class First{
    
    File myFile;
    public static void main(){
    
    Second parser = new Second(this.myFile);
    parser.dfcparse();
    
    }
    }


    Second class: array is created and contains some lines from the text file.

    Java Code:
    public Class Second{
    
    File toParse;
    Scanner scanner;
    
    public Second(File toParse) throws FileNotFoundException{
            this.toParse = toParse;
            this.scanner = new Scanner(new FileReader(this.toParse));
            }
    public void dfcparse() throws ArrayIndexOutOfBoundsException
                {
            
            tn=new String[900];
            int i=1;
            while(this.scanner.hasNextLine())
    {
                tmp = this.scanner.nextLine(); // declaring temp to the  
                this.tn[i]=tmp;
                i++;
                
    }
               this.scanner.close();
        }
    
    
    }

    Third class: which doesn't have a main function. How could I get this tn[] array in this third class.

    Java Code:
    public class Third{
    
    ????????????????
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    Where is the tn array defined?
    In that class you could add a method that returns a reference to the tn array.

  3. #3
    lemontree45 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Thank you Norm for the quick response.

    tn array is defined in the second class.

    How do you create that kind of a reference method? can you give me an example or sth?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    tn array is defined in the second class.
    That's funny, when I do a search I can't find any definition for the array. Does your posted code compile ok?

    A get method:
    String[] getTN() {return tn;}

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  2. help adding to array from another class
    By Grid_iso in forum New To Java
    Replies: 0
    Last Post: 05-09-2011, 10:00 AM
  3. calling a 2D Array from a different class
    By straw in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 12:29 AM
  4. How do I retrieve an array from a different class?
    By Psyclone in forum AWT / Swing
    Replies: 11
    Last Post: 02-08-2010, 09:52 AM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •