重用StringBuilder和ArrayIndexOutOfBoundsException

时间:2017-04-03 16:18:39

标签: java indexoutofboundsexception stringbuilder

我最近开始学习Java,但我遇到了StringBuilder类的问题。 我有一个StringBuilder实例,在我向它添加许多字符串后,我想清空它以附加一个新序列。

我尝试过很多不同的事情,例如为该变量分配一个新的StringBuilder,deletesetLength以及所有这些,但我得到了异常ArrayIndexOutOfBoundsException

为什么我会收到此异常?我怎样才能有效地完成这项任务?

编辑:所以详细说明我正在尝试编写像Windows caculate app这样的计算应用程序,主要的是流程变量将存储数字和操作,所以当它需要有一个结果时它会cal计算方法来做到这一点。它的工作方式与window caculate app完全相同。如果我继续重复执行“+”操作,它会正常工作,它会在点击操作按钮后更新结果,但是当点击“=”按钮时,它会在过程变量处获得异常ArrayIndexOutOfBoundsException。我知道我的代码很复杂,希望你们能找到解决方案,我已经尝试了你们推荐的所有解决方案,所有这些ges异常

public class Cal extends javax.swing.JFrame {

/**
 * Creates new form Cal
 */



    StringBuilder process ;
    StringBuilder textOutcome ;

    boolean isResult; // it means that you just hit the operation button or "=" button

    boolean isRestart;// it means after you hit "=" button if after that you continue hit the operation button that will false ,if you hit numbe button it will true
     public double caculate ()
    {

        String[] split = process.toString().split(" ");
        double result = Double.valueOf(split[1]);
        for(int i = 2 ; i < split.length;i++)
        {
            if(split[i].equals("+"))
            {
              result += Double.valueOf(split[i + 1]);
              i += 1;
            }


        }
        return result;


    }
public Cal() {
    initComponents();
     process = new StringBuilder();
   textOutcome =  new StringBuilder();

    isResult = true;
   isRestart = false;
    GridLayout grid = new GridLayout(4,4,10,10);


    JButton btnAdd = new JButton("+");
       btnAdd.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
           //To change body of generated methods, choose Tools | Templates.
           txtOutcome.setText(String.valueOf(caculate()));
           process.append( " "+e.getActionCommand());
           txtProcess.setText(process.toString());
           isResult = true;
           isRestart = false;





        }
    });

    pGrid.add(btnAdd);
     JButton btn4 = new JButton("4");
     btn4.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(isResult)
               {
                   isResult = false;
                   textOutcome = new StringBuilder();
                   textOutcome.append( e.getActionCommand());
                   txtOutcome.setText(textOutcome.toString());
                   if(isRestart)
             {
                 process = new StringBuilder();

                   isRestart = false;
             }
                   process.append( " "+e.getActionCommand());



               }
             else
             {

                  textOutcome.append( e.getActionCommand());
                   txtOutcome.setText(textOutcome.toString());

                   process.append(e.getActionCommand());

             }

        }
    });
      pGrid.add(btn4);
      JButton btnResult = new JButton("=");
      btnResult.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
             txtOutcome.setText(String.valueOf(caculate()));
          process.delete(0, process.length());

              process.append(String.valueOf(caculate()));
                txtProcess.setText(process.toString());
              isResult = true;
              isRestart = true;

        }
    });

    pGrid.add(btnResult);
      pGrid.setLayout(grid);
}

// Variables declaration - do not modify                     
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel pGrid;
private javax.swing.JTextField txtOutcome;
private javax.swing.JTextField txtProcess;
// End of variables declaration                   

}

2 个答案:

答案 0 :(得分:0)

请尝试创建一个新的空StringBuilder

sb = new StringBuilder("");

答案 1 :(得分:0)

    StringBuilder sB = new StringBuilder();
    sB.append("Aaa");
    sB.append("Bbb");
    sB.append("Ccc");
    System.out.println(sB); // prints: AaaBbbCcc

    sB.delete(0, sB.length());
    sB.append("Ddd");
    System.out.println(sB); //prints: Ddd