Class Passing and Returning Arrays

Thread Solved

Join Date: Apr 2004
Posts: 7
Reputation: Ronnie is an unknown quantity at this point 
Solved Threads: 0
Ronnie Ronnie is offline Offline
Newbie Poster

Class Passing and Returning Arrays

 
1
  #1
May 6th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 1
Reputation: bizKwiK is an unknown quantity at this point 
Solved Threads: 1
bizKwiK bizKwiK is offline Offline
Newbie Poster

Re: Class Passing and Returning Arrays

 
1
  #2
May 7th, 2004
You want to creat an object of the type "Climate" within your class1. Then you can use its methods.

Climate cl= new Climate();
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 6
Reputation: sykkn is an unknown quantity at this point 
Solved Threads: 1
sykkn sykkn is offline Offline
Newbie Poster

Re: Class Passing and Returning Arrays

 
1
  #3
May 26th, 2004
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[].
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 6
Reputation: sykkn is an unknown quantity at this point 
Solved Threads: 1
sykkn sykkn is offline Offline
Newbie Poster

Re: Class Passing and Returning Arrays

 
0
  #4
May 26th, 2004
Review the following

CLASS I (Partical)
  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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 7
Reputation: Ronnie is an unknown quantity at this point 
Solved Threads: 0
Ronnie Ronnie is offline Offline
Newbie Poster

Re: Class Passing and Returning Arrays

 
0
  #5
Jun 1st, 2004
Thanks...It works. Ronnie
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 7
Reputation: Ronnie is an unknown quantity at this point 
Solved Threads: 0
Ronnie Ronnie is offline Offline
Newbie Poster

Re: Class Passing and Returning Arrays

 
0
  #6
Jun 1st, 2004
Thanks...I got it to work.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC