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 = st.nextToken();
i++;
}
}
}


}
}

Thanks...Ronnie

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

Climate cl= new Climate();

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[].

Review the following

CLASS I (Partical)

.
.
.
public String climateDataOut[];

try
 {
  climateDateOut = 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 String[] 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++;
    }
   }
  }
  return(climateDataOut);
 }
}

Thanks...It works. Ronnie

Thanks...I got it to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.