| | |
Class Passing and Returning Arrays
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2004
Posts: 7
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2004
Posts: 6
Reputation:
Solved Threads: 1
Review the following
CLASS I (Partical)
CLASS II
Reads data O.K.
CLASS I (Partical)
Java Syntax (Toggle Plain Text)
. . . 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.
Java Syntax (Toggle Plain Text)
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); } }
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: JDBC connection
- Next Thread: chat in java
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows





