ArrayLists issue and file reading
I am having difficulty with the following method. I can't figure out if my problem is there, but I've narrowed it down to not populate the list of arrays from a file. Any help is appreciated.
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
//create arraylists
ArrayList<String> model = new ArrayList<String>();
ArrayList<String> length = new ArrayList<String>();
ArrayList<String> width = new ArrayList<String>();
ArrayList<String> radius = new ArrayList<String>();
ArrayList<String> depth = new ArrayList<String>();
ArrayList<String> volume = new ArrayList<String>();
ArrayList<String> shape = new ArrayList<String>();
//fill arraylists from file
try {
String outputline = "";
BufferedReader fin = new BufferedReader(new FileReader("stock.dat"));
while((outputline = fin.readLine()) != null) {
// for(int i = 0; i < outputline.length(); i++) {
int i = 0;
//model
boolean flag = false;
String pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
i++;
}
model.add(pass);
//length
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
length.add(pass);
//width
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
width.add(pass);
//radius
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
radius.add(pass);
//depth
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
depth.add(pass);
//volume
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
volume.add(pass);
//shape
pass = "";
for(int j = i; j < outputline.length(); j++)
pass.concat(Character.toString(outputline.charAt(i)));
shape.add(pass);
}
fin.close();
}
catch(IOException e) {
System.err.print("Unable to read from file");
System.exit(-1);
}
int at = -1;
for(int i = 0; i < model.size(); i++) {
if(model.get(i).equals(searchIn.getText())) {
at = i;
i = model.size();
}
}
Component frame = null;
if(at != -1) {
searchDepthOut.setText(depth.get(at));
searchLengthOut.setText(length.get(at));
searchRadiusOut.setText(radius.get(at));
searchVolumeOut.setText(volume.get(at));
searchWidthOut.setText(width.get(at));
}
else
JOptionPane.showMessageDialog(null, "Your search did not return any results", "ERORR", JOptionPane.ERROR_MESSAGE);
}
Separate readline with comma and do with it. I would also create an object for the model, length, width, etc., and then have 1 arraylist of that object.
while((outputline = fin.readLine()) != null) {
String[] tokens = outputline.split(",");
if(tokens.length == 7){
SObj o = new SObj; //Some Object
o.model = tokens[0];
o.length = tokens[1];
//and so on
oList.add(o);
}
}
source to share
I would suggest you restructure your code. The problem is not only that there is some kind of parser error, but that it is difficult to tell what is going on - the code is clearly making assumptions about the structure of the input string, but you need to read and trace the method of restructuring it in your mind.
Something like
/** Expect a line of the form model, length, ...,
return a list of ...
*/
private String[] parse (String inputLine)
{
//check input line charachteristics-not null, length, ...
String out= inputLine.split(",");
if (out.length()!= ...
//whatever sanity checking...
}
private List<String[]> extract(BufferedReader fin)
{
while((outputline = fin.readLine()) != null)
{
//do something with parse(outputline);
}
}
A useful thing would be to separate the file reading and the parsing of the line so that instead of doing it all in a long sequence, you can see what breaks, most likely an assumption about the structure of the line, which is similar to code. Do you need 4 semicolon integers? five? how about if they are filled with spaces? empty string prefix?
source to share