another array to use in another class?
Hi. I have a reader class (code below) that just reads over a text file and puts items into a comma-delimited array. Items in the text file are int positionCode and String positionTitle, in the form:
102,Manager
248,DeptHead
299,Assistant
etc...
My question is: Is there a way to put ONLY the position codes (int) into a separate array?
**I want to write a method in another class that finds the position code with the lowest value.**
Is putting them in a separate array the best way or should I do it another way and how?
Code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* reads in an employee's position in a department into memory
*/
public class PositionReader {
private BufferedReader in;
private Company Company;
/**
* constructor
* @param fileName
* @throws FileNotFoundException
*/
public PositionReader(String fileName, Company company) throws FileNotFoundException {
in = new BufferedReader(new FileReader(fileName));
this.Company = company;
}
/**
* reads a position from text file
* @return position
*/
public Position readPosition() {
String line;
try {
line = in.readLine();
if (line == null || line.trim().length() == 0) {
return null;
}
String[] fields = line.split(",");
int positionCode = Integer.parseInt(fields[0]);
String positionTitle = fields[1];
Position position = new Position(positionCode, positionTitle);
return position;
} catch (IOException e) {
return null;
}
}
public void close() throws IOException {
in.close();
}
}
Re: another array to use in another class?
To put the codes into an array, you need to know how many there are.
Where do you store the Position records that are returned by readPosition? Can the size of that be used to create the array and then make a pass over the Position records and extract the codes to the array.
Why do you need a separate array of codes? Can your search routine look in the Position records for the value desired? That would make more sense.