Java将存储在arraylist中的字符串解析为计算

时间:2014-06-07 16:50:05

标签: java string math arraylist double

所以,我有一个ArrayList存储信息,如:{“2.0”,“+”,“2.0”,“ - ”,“1.0”}我需要将其解析为2 + 2 - 1,但是我做的方法不起作用。

方法代码:

public static void ans()
{
    Double cV = Double.parseDouble(calculate.get(0));

    for(int i = 1; i < calculate.size(); i += 2)
    {

        switch(calculate.get(i))
        {
            case "+":
                cV += Double.parseDouble(calculate.get(i + 1));
                break;
            case "-":
                cV -= Double.parseDouble(calculate.get(i + 1));
                break;
        }
    }

    calc.setText("= " + cV);
}

“计算”这里是我的arrayList。

它做错了只是返回第一个数而不是计算的答案。任何帮助将不胜感激!

编辑:我添加了System.out.print(calculate.get(i)+“,”+ calculate.get(i + 1)+“,”);进入for循环并且没有发生任何事情...由于某种原因,循环没有运行。

编辑:完整代码:http://pastebin.com/cP3hGgA3

编辑:所以我刚添加:System.out.println(calculate.size());进入方法,它返回1 ...发生了什么?

编辑:我认为问题在于:

public static void addTo(String toAdd)
{
    try{
        if(!isNumeric(toAdd))
        {
            if(!isNumeric(calc.get(calc.size() - 1)))
            {
                calc.set(calc.size() - 1, toAdd);
            }
        }else{
            calc.add(toAdd);
        }
    }catch(Exception e){ }
}

public static boolean isNumeric(String str)  
{  
    try{  
        Double.parseDouble(str);  
    }catch(NumberFormatException nfe){  
        return false;  
    }
    return true;  
}

编辑:短代码:

    package net.discfiresoftworks.shortcalc;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Short extends JFrame
{
    private static final long serialVersionUID = 1L;

    public static ArrayList<String> calc = new ArrayList<String>();

    public static JLabel ans = new JLabel("");

    public static void main(String[] args)
    {
        new Short();
    }

    public Short()
    {
        this.setSize(300, 300);
        this.setDefaultCloseOperation(3);
        this.setLayout(new FlowLayout());

        JButton b1 = new JButton("Click me");
        JButton b2 = new JButton("then me");
        JButton b3 = new JButton("then me.");

        b1.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                addTo("1");
            }

        });

        b2.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                addTo("+");
            }

        });

        b3.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                addTo("1");
                ans();
            }

        });

        this.add(b1);
        this.add(b2);
        this.add(b3);
        this.add(ans);

        this.setVisible(true);
    }

    public static void addTo(String toAdd)
    {
        try{
            if(!isNumeric(toAdd))
            {
                if(!isNumeric(calc.get(calc.size() - 1)))
                {
                    calc.set(calc.size() - 1, toAdd);
                }
            }else{
                calc.add(toAdd);
            }
        }catch(Exception e){ }
    }

    public static boolean isNumeric(String str)  
    {  
        try{  
            Double.parseDouble(str);  
        }catch(NumberFormatException nfe){  
            return false;  
        }
        return true;  
    }

    public static void ans()
    {
        Double cV = Double.parseDouble(calc.get(0));

        System.out.println(calc.size());

        for(int i = 1; i < calc.size(); i += 2)
        {

            switch(calc.get(i))
            {
                case "+":
                    cV += Double.parseDouble(calc.get(i + 1));
                    break;
            }
        }

        ans.setText("= " + cV);
    }
}

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题(我认为calculate{"2.0", "+", "2.0", "-", "1.0"}形式的数组:

public static void ans() {
    Double total = 0.0;
    boolean isSum = true;

    for (String input : calculate) {
        switch (input) {
            case "+":
                isSum = true;
                break;
            case "-":
                isSum = false;
                break;
            default:
                if (isSum)
                    total += Double.parseDouble(input);
                else 
                    total -= Double.parseDouble(input);
                break;
        }
    }

    /*
    * Now you have the total variable which stores the sum. 
    * You can do whatever you want with it, like printing
    * it along with the result.
    */
    for (String input : calculate) {
        System.out.print(input+" ");
    }
    System.out.print(" = "+total);

}

但是,如果您有这样的数组,这将无效(如预期的那样):

{"2.0", "+", "5.0", "7.0"}

因为它将57相加,因为它存储了您使用的最后一个符号,您可能希望实现某种需要数字之间符号的vallidation方法。但是如果你确定你的输入总是number, symbol, number那么这个代码就没问题了。