Java ArrayList和读取文件时出现问题

Java ArrayList和读取文件时出现问题,java,arraylist,Java,Arraylist,我对以下方法有困难。我不知道我的问题是否是,但我已经缩小了范围,没有从文件中填充数组列表。非常感谢您的帮助 private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) { //create arraylists ArrayList<String> model = new ArrayList<String>(); ArrayList<String> length = new

我对以下方法有困难。我不知道我的问题是否是,但我已经缩小了范围,没有从文件中填充数组列表。非常感谢您的帮助

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);
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt){
//创建阵列列表
ArrayList模型=新的ArrayList();
ArrayList长度=新的ArrayList();
ArrayList宽度=新的ArrayList();
ArrayList半径=新的ArrayList();
ArrayList深度=新建ArrayList();
ArrayList volume=新建ArrayList();
ArrayList形状=新的ArrayList();
//从文件中填充ArrayList
试一试{
字符串outputline=“”;
BufferedReader fin=新的BufferedReader(新文件阅读器(“stock.dat”);
而((outputline=fin.readLine())!=null){
//对于(int i=0;i

}

除了人们列出的所有其他问题之外

String pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
   pass.concat(Character.toString(outputline.charAt(i)));
pass是一个字符串。字符串是不可变的。是否要

   pass = pass.concat(.....)

用逗号分割读线,然后处理它。我还要为模型、长度、宽度等创建一个对象,然后为该对象创建一个arraylist

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);
    }
}

while(flag=false)
永远不会运行-它的计算结果总是
false
。请尝试
while(!flag)

我建议您重新构造代码。问题不仅在于存在某种解析器错误,还在于很难判断到底发生了什么-代码显然是在对输入行的结构进行假设,但您必须在脑海中通读并跟踪重新构造的方法

差不多

/** 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);
  }
}
/**需要一行模型、长度。。。,
返回…的列表。。。
*/
私有字符串[]解析(字符串输入行)
{
//检查输入行特征不为空,长度。。。
String out=inputLine.split(“,”);
如果(out.length()!=。。。
//不管是什么精神检查。。。
}
私有列表摘录(BufferedReader fin)
{
而((outputline=fin.readLine())!=null)
{
//用parse(outputline)做一些事情;
}
}

有用的方法是将文件读取和行解析分离开来,这样你就可以看到破坏性的东西,而不是在一个长序列中进行所有操作,很可能是关于代码中隐藏的行结构的假设。它是否需要4个逗号分隔的整数?5?如果它们填充了空格如何?前缀为空行?

你能发布stock.dat的内容吗?如果很长,你能隔离出导致问题的行吗?也许你能告诉我们期望的行为是什么吗?我很抱歉这么说,但是你发布的代码非常糟糕,需要进行彻底的重构。创建一个类来表示7个属性,而不是7个平行属性l ArrayList,并使用String.split解析逗号分隔的列表。负责的IDE应在条件中将此赋值标记为布尔值。IDE的存在是为了简化工作,而不是猜测代码。它是一个完全有效的条件,可能是故意存在的。