-2

So, I have an ArrayList that stores information like: {"2.0", "+", "2.0", "-", "1.0"} and I need to parse that into 2 + 2 - 1, however the method I made to do that doesnt work.

Method Code:

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

"calculate" here is my arrayList.

What it is doing wrong is just returning the first number rather than the answer to the calculation. Any help would be appreciated!

EDIT: I added System.out.print(calculate.get(i) + ", " + calculate.get(i + 1) + ", "); into the for loop and nothing is happening... For some reason the loop isn't getting run.

EDIT: Full Code: http://pastebin.com/cP3hGgA3

EDIT: So I just added: System.out.println(calculate.size()); into the method, and it is returning 1... What is going on?

EDIT: I think the problem is here:

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

EDIT: Short Code:

    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 Answers1

0

This should do the trick (I suupose that calculate is the array in the form {"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);

}

However, this will not work (as expected) if you have an array like this:

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

As it will sum the 5 and the 7 because it stores the last sign you used, you may want to implement some sort of vallidation method that requires symbols between numbers. But if you're sure your input will always be number, symbol, number then it'll no problem with this code.

arielnmz
  • 8,354
  • 9
  • 38
  • 66