Arrays Classes Passing / Returning


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Arrays Classes Passing / Returning

  1. #1
    Join Date
    Sep 2003
    Posts
    36

    Arrays Classes Passing / Returning

    I have two classes. The first collects input data and calls a second class for inputing data from a file. After the data is read I would like to use it inother classes but am having problems getting the data back.



    To save space I will only show part of class I (calling class) which works, and all of class II that inputs the data O.K., but does not return it to class I:



    CLASS I (Partical)

    .

    .

    .

    try
    {
    Climate.climaticData(County); // this works
    }
    catch (FileNotFoundException e)
    {
    }
    catch (IOException e)
    {
    }



    System.out.println(climateDataOut[3]); // Does not print element 3

    .

    .

    .





    CLASS II

    Reads data O.K.



    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.StringTokenizer;
    /**
    #######################################################
    # @author RON_C
    #######################################################
    */
    public class Climate
    {
    /**
    * Method LineOfStars.
    * @param string
    */
    public static void climaticData(String County)
    throws FileNotFoundException, IOException
    {
    System.out.println(County + " Second File");
    String[ ] climateDataOut = new String[45];
    int i = 0;
    String climate = null;
    BufferedReader brclimate =
    new BufferedReader(new FileReader("d:\\Java Info\\Jclimate.txt"));
    while ((climate = brclimate.readLine()) != null)
    {
    if (County.substring(0, 4).equals(climate.substring(0, 4)))
    {
    StringTokenizer st = new StringTokenizer(climate, ",", false);
    while (st.hasMoreTokens())
    {
    climateDataOut[i] = st.nextToken();
    i++;
    }
    }
    }

    }
    }




    Thanks...Ronnie

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    tell me.. what stops your program crashing if it finds 46 strings in the file that it wants to store in the array?

    climateDataOut[3] will contain no data if no data is found.. how can you be sure that data is being found?

    (String County) should read String county. Do not start varaible names with a capital letter
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Sep 2003
    Posts
    36
    I know the number of items is fixed at 45 so that is not the problem I think that the problem might relate to local vs global variables.

    The program does not crash, it just does not produce any output.

    By the want I want to thank you for the GridbagLayout Tutorial. Its been a great resource.

    Ronnie

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    the method is void, it doesn't return anything.
    Code:
    System.out.println(climateDataOut[3]); // Does not print element 3
    the array climateDataOut does not exist here. It is declared inside a method (a method in a different class so even if you COULD access it you'd need to specify which class it was in), so it only exists (ie has memory space) while the method is running. Once the method ends it ceases to exist.

    You need to alter the method you're calling so it returns an array of strings. As cjard noted it isn't a good idea to write a method that only works for a specific number in case you change how you do it in the future. You may change the number of strings in the file for instance. Even if you never do, its best to get into the habit of writing code that is as generic as possible, ie when using arrays you should use array.length to loop through it.

  5. #5
    Join Date
    Sep 2003
    Posts
    36
    Thanks for the suggestions...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center