-
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
-
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
-
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
-
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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks