随机丢失长度()值2

时间:2017-01-12 19:35:25

标签: java string arraylist string-length

我正在研究一个学校项目,我已经创建了这个算法来处理输入的计算。我想保留过去的公式显示在屏幕上,但是用户可以根据需要删除它们,但是在我打电话的3个方程式之后EQUATIONs.size()。length()得到最后完成的等式的长度,长度偏离2导致误差,导致等式最后输入与先前的输入连接,防止等式被处理。

public class gt{

ArrayList<String> EQUATIONs = new ArrayList<String>();          
String EQUATION = "";                               

public void setEquationList(String E)
{
    ArrayList<String> TEMPE = new ArrayList<String>();
    System.out.println("LSave " + EQUATIONs.toString());
    if(EQUATIONs.size() == 0)                           //adds directly
    {
        EQUATIONs.add(E);
        System.out.println("E empty " + EQUATION);
    }
    else{
        int lengthTotal = 0, x = 0, z = 0;

        while(x < EQUATIONs.size())             
        {
            if(!E.contains(EQUATIONs.get(x)))           //if input does not match value in EQUATIONs it is removed (if only 1 element removed)
                EQUATIONs.remove(x);
            x++;
        }
        if(EQUATIONs.size() == 0)                           //adds directly
        {
            EQUATIONs.add(E);
            System.out.println("E empty " + EQUATION);
        }
        while(z < EQUATIONs.size())
        {
            if(lengthTotal == 0)                        //2nd equation added in will be processed here
            {
                TEMPE.add(E.substring(lengthTotal, EQUATIONs.get(z).length() + lengthTotal).trim());
                System.out.println("T+ == " + TEMPE.get(z) + " | ( " + lengthTotal + ", " + (lengthTotal + TEMPE.get(z).length()) +" )");
                System.out.println("T: " + TEMPE.toString()+"\n");
            }
            if(lengthTotal != 0)
            {
                TEMPE.add(E.substring(lengthTotal + 1, lengthTotal + EQUATIONs.get(z).length() + 1).trim());
                System.out.println("T+ != " + TEMPE.get(z) + " | ( " + (lengthTotal + 1) + ", " + (lengthTotal + TEMPE.get(z).length()) +" )");
                System.out.println("T: " + TEMPE.toString() +"\n");
            }

            System.out.println("LT inc = " + lengthTotal + " + " + EQUATIONs.get(z).length() + " = " + (lengthTotal + + EQUATIONs.get(z).length()));

            lengthTotal = lengthTotal + EQUATIONs.get(z).length();

            if(!TEMPE.get(z).equals(EQUATIONs.get(z)))
            {
                lengthTotal = lengthTotal - EQUATIONs.get(z).length();      //to recheck last attempt
                System.out.println("LT-LT+E.g(l) \nEremove " + EQUATIONs.get(z));
                EQUATIONs.remove(z);
                System.out.println("Tremove " + TEMPE.get(z));
                TEMPE.remove(z);
                z--;                                                        //to recheck last attempt
            }

            z++;
        }

        System.out.println("LT @ " + lengthTotal);
        EQUATIONs.add(E.substring(lengthTotal + 1).trim());
        System.out.println("E ladd " + EQUATIONs.get(EQUATIONs.size()-1));
    }
    System.out.println("EQUATIONs Final " + EQUATIONs.toString() + "\n------------\n");
}
}

和界面     import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextPane;
import java.awt.Color;

import javax.swing.border.LineBorder;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JScrollPane;

public class gt extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                gt frame = new gt();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public gt() {
    Calcs C = new Calcs();
    addWindowListener(new WindowAdapter() {
        public void windowOpened(WindowEvent e) {
            try {
                C.getVar();
            } catch (IOException e1) {                  
                C.FileNotFound();
            }
        }
        public void windowClosed(WindowEvent e) {
            C.saveAllVar();
        }
    });

    setTitle("Calculator");
    setBackground(Color.BLACK);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1000, 700);
    contentPane = new JPanel();
    contentPane.setBackground(Color.BLACK);                 //add customization for colour || picture upload
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel Calculator = new JPanel();
    Calculator.setBorder(new LineBorder(Color.LIGHT_GRAY, 2, true));
    Calculator.setBackground(Color.DARK_GRAY);
    Calculator.setBounds(0, 0, 302, 661);
    contentPane.add(Calculator);
    Calculator.setLayout(null);

    JScrollPane scrollPane_1 = new JScrollPane();
    scrollPane_1.setBounds(10, 22, 281, 209);
    Calculator.add(scrollPane_1);

    JTextPane Calc = new JTextPane();
    scrollPane_1.setViewportView(Calc);
    Calc.addKeyListener(new KeyAdapter() {  //use array list to store each line
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER)
            {
                C.EQUATION = Calc.getText();
                C.setEquationList(C.EQUATION);
                /*try { //removed as I am not including
                    C.cycle(C.EQUATIONs.get(C.EQUATIONs.size() - 1));
                } catch (IOException e1) {
                    C.FileNotFound();
                    e1.printStackTrace();
                }*/
                C.toPrint();
                Calc.setText("");
                Calc.setText(C.EQUATION);       //So that all previous math are retained for user use with "-" being detection char for what line we are using
            }
        }
    });
    Calc.setText("");
}
}

0 个答案:

没有答案