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