943,678 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 17250
  • Java RSS
May 6th, 2004
1

Class Passing and Returning Arrays

Expand Post »
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
Similar Threads
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Ronnie is offline Offline
13 posts
since Apr 2004
May 7th, 2004
1

Re: Class Passing and Returning Arrays

You want to creat an object of the type "Climate" within your class1. Then you can use its methods.

Climate cl= new Climate();
Reputation Points: 11
Solved Threads: 1
Newbie Poster
bizKwiK is offline Offline
1 posts
since May 2004
May 26th, 2004
1

Re: Class Passing and Returning Arrays

you need to change your climaticData method to return String[] instead of void. Add return(climateDataOut); at the end of the method and then change your calling class to catch the String[].
Reputation Points: 12
Solved Threads: 1
Newbie Poster
sykkn is offline Offline
6 posts
since Apr 2004
May 26th, 2004
0

Re: Class Passing and Returning Arrays

Review the following

CLASS I (Partical)
Java Syntax (Toggle Plain Text)
  1. .
  2. .
  3. .
  4. public String climateDataOut[];
  5.  
  6. try
  7. {
  8. climateDateOut = Climate.climaticData(County); // this works
  9. }
  10. catch (FileNotFoundException e)
  11. {
  12. }
  13. catch (IOException e)
  14. {
  15. }
  16.  
  17.  
  18. System.out.println(climateDataOut[3]); // Does not print element 3
  19. .
  20. .
  21. .

CLASS II
Reads data O.K.

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.StringTokenizer;
  7. /**
  8.  #######################################################
  9.  # @author RON_C
  10.  #######################################################
  11.   */
  12. public class Climate
  13. {
  14. /**
  15.  * Method LineOfStars.
  16.  * @param string
  17.  */
  18. public static String[] climaticData(String County)
  19. throws FileNotFoundException, IOException
  20. {
  21. System.out.println(County + " Second File");
  22. String[ ] climateDataOut = new String[45];
  23. int i = 0;
  24. String climate = null;
  25. BufferedReader brclimate =
  26. new BufferedReader(new FileReader("d:\\Java Info\\Jclimate.txt"));
  27. while ((climate = brclimate.readLine()) != null)
  28. {
  29. if (County.substring(0, 4).equals(climate.substring(0, 4)))
  30. {
  31. StringTokenizer st = new StringTokenizer(climate, ",", false);
  32. while (st.hasMoreTokens())
  33. {
  34. climateDataOut[i] = st.nextToken();
  35. i++;
  36. }
  37. }
  38. }
  39. return(climateDataOut);
  40. }
  41. }
Reputation Points: 12
Solved Threads: 1
Newbie Poster
sykkn is offline Offline
6 posts
since Apr 2004
Jun 1st, 2004
0

Re: Class Passing and Returning Arrays

Thanks...It works. Ronnie
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Ronnie is offline Offline
13 posts
since Apr 2004
Jun 1st, 2004
0

Re: Class Passing and Returning Arrays

Thanks...I got it to work.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Ronnie is offline Offline
13 posts
since Apr 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JDBC connection
Next Thread in Java Forum Timeline: chat in java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC