tags:

views:

252

answers:

7

I am having difficulty with the following method. I can't figure out if my problem is, but I have narrowed it down to not populating the array list from the file. Any help is greatly 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);

}

A: 

So what problem exactly are you getting? Your question isn't that clear.

Exception? Unexpected result? What is the expected result?

madlep
A: 

expected result is to display values based on a search of the model array list. The file is stored

model, length, width, radius, depth, volume, shape

It is currently displaying the error message instead. I believe my problem is that my logic to retrieve the values and place them into the array lists is wrong.

+3  A: 

Aside from all the other problems people have listed...

String pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
   pass.concat(Character.toString(outputline.charAt(i)));

pass is a String. Strings are immutable. You want

   pass = pass.concat(.....)
jmucchiello
+4  A: 

Split the readline by a comma and be done with it. I'd 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);
    }
}
dotjoe
This is absolutely correct, and IMHO should be the accepted answer.
nsayer
+2  A: 

while(flag = false) will never be run - it always evaluates to false. Try while (!flag)

Max Stewart
Responsible IDEs should flag this assignment to boolean within conditional.
Dov Wasserman
IDE's are there to make the job easier, not second guess the code. It's a perfectly valid condition that may be there on purpose.
Feet
A: 

I would suggest you restructure the code. The issue is not just that there's some sort of parser error, but that it's sort of hard to tell what's going on - the code is clearly making assumptions about the structure of the input line, but you have to kind of read through and trace the method to restructure 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);
  }
}

The helpful thing will be to separate out the file reading and the line parsing, so that instead of doing it all in a long sequence you can see what's breaking, most likely an assumption about the line structure that's buried in the code. Does it need 4 comma separated integers? 5? how about if they're padded with spaces? prefixed with an empty line?

Steve B.
A: 

Thanks everyone for your help. Got it working with Joe's suggestion.

You should probably mark Joe's answer as The Answer.
Ken Paul