This problem takes a bit of explaining, I’ll try to be as concise as possible:
I have am trying to initalise an array of Can objects, these objects only have 2 fields (both Strings): name, manufacturer
I am trying to initialise the fields by reading from a CSV file with the following format:
Tomatoes,Heinz
Legumes,Jerry
(no space between the lines, it’s being formatted like that on this site for some reason)
The first string in each row is the value I want to be the name, the 2nd is the manufacturer.
So I’ve created a method to read each line of the CSV, which passes each line to a tokenizer method to extract single values:
private void readFile (String inFilename) {
FileInputStream fileStrm = null;
InputStreamReader rdr;
BufferedReader bufRdr;
int lineNum;
String line;
try {
fileStrm = new FileInputStream(inFilename);
rdr = new InputStreamReader(fileStrm);
bufRdr = new BufferedReader(rdr);
lineNum = 0;
line = bufRdr.readLine();
while {line != null) {
lineNum++;
processLine(line); //passes line to tokenizer
line = bufRdr.readLine();
}
fileStrm.close();
}
catch (IOException e) {
if (fileStrm != null) {
try { fileStrm.close(); } catch (IOException ex2) { }
}
System.out.println("Error in file processing: " + e.getMessage());
}
}
The lines are passed to this tokenizer method:
private String processLine(String csvRow) {
String thisToken = null;
StringTokenizer strTok;
strTok = new StringTokenizer(csvRow, ",");
while (strTok.hasMoreTokens()) {
thisToken = strTok.nextToken();
}
}
And that’s where I get a bit stuck. To initialise my array I think I’d need a for loop, something like
for (int i=0; i<=array.length;i++)
{
array[i].name = readFile("filename.csv");
array[i].manufacturer = readFile("filename.csv");
}
But obviously this will not work. Can anyone suggest how I can go about this? I’d prefer to keep the code mostly intact and figure out a solution using the existing code.
Thanks
First thing: –
You are calling
processLine(line);, but are not returning thetokenread from this method.. So, the token obtained in this method inengulpedthere only.. So, you should return something from that method..Second:-
In the above code, you are calling
readFile()each time for the two attributes.. So, even if you return somthing, these two attributes will be initialized to same value.. Because each time you are starting reading file from scratch..Third thing: –
In fact your above code will not compile.. Because you are assigning the value of
readFile()(which is actually not returning anything) to array.. So give a return type to this method.. It would beString.. And returning the tokens read..EDIT: –
* I would suggest, you can use split() method of
Stringclass..Tokenizer is not needed here, for justsplittingaround a singlecomma(,)`Also, rather than using an array, you can use ArrayList, in which you can add your newly created
objecton the fly.. That way, you will not have tofix the sizeof array.. (And this is what you will want, as you don’t know how much line you will have in your file right?)Here’s what you can do: –
Call the method
readFilefrom somewhere, probablymain()In your
readFile()method, you can iterate over file to create anArrayListlike this: –I assume,
Canis the name of your class as you stated in your problem..