【问题标题】:Problem with ArrayLists and reading a fileArrayLists 和读取文件的问题
【发布时间】:2010-09-26 13:53:15
【问题描述】:

我对以下方法有困难。我不知道我的问题是否存在,但我已将其范围缩小到不从文件中填充数组列表。非常感谢任何帮助。

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

}

【问题讨论】:

  • 你能把stock.dat的内容贴出来吗?如果它很长,您能否隔离导致问题的行。也许你能告诉我们期望的行为是什么?
  • 我很抱歉这么说,但您发布的代码简直太糟糕了,需要拼命重构。创建一个类来表示 7 个属性而不是 7 个并行数组列表,并使用 String.split 解析逗号分隔的列表。

标签: java arraylist


【解决方案1】:

用逗号分割 readline 并完成它。我还会为模型、长度、宽度等创建一个对象......然后拥有该对象的 1 个数组列表。

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

【讨论】:

    【解决方案2】:

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

    类似

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

    有帮助的是将文件读取和行解析分开,这样您就可以看到发生了什么问题,而不是按顺序执行所有操作,这很可能是对隐藏在代码中的行结构的假设。它需要 4 个逗号分隔的整数吗? 5?如果他们用空格填充呢?以空行为前缀?

    【讨论】:

      【解决方案3】:

      while(flag = false) 永远不会运行 - 它总是评估为false。试试while (!flag)

      【讨论】:

      • 负责任的 IDE 应在条件范围内将此分配标记为布尔值。
      • IDE 的存在是为了让工作更轻松,而不是猜测代码。这是一个完全有效的条件,可能是故意存在的。
      【解决方案4】:

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

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

      pass 是一个字符串。字符串是不可变的。你想要的

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-11
        • 2017-07-16
        • 2014-04-18
        • 2014-01-05
        相关资源
        最近更新 更多