Here is my code:
import java.util.ArrayList;
public class SplitString
{
public static void main(String[] args)
{
String s = "80+5+3*(11%(3*2)-(5+1)+6)-(10+10)+(2*2)*5";
ArrayList<String> equation = new ArrayList<>();
String ns = "";
String b;
int nsi;
for(int c=0; c<s.length(); c++)
{
b = s.substring(c,c+1);
switch (b) {
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
ns += b;
break;
case "+":
case "-":
case "*":
case "/":
case "%":
case "(":
case ")":
nsi = Integer.parseInt(ns);
equation.add(Integer.toString(nsi));
equation.add(b);
ns = "";
break;
}
for(int i=0; i<equation.size(); i++)
{
System.out.print(equation.get(i));
}
}
}
}
I'm trying to split the String s into separate numbers and operators within an ArrayList
in such a way that if a number like 00080
is entered, it will assume the number as 80
instead. But I'm having trouble, and when I run this it gives me:
Exception in thread "main" java.lang.NumberFormatException: For input string: "" 80+80+80+5+80+5+80+5+3* at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:504) at java.lang.Integer.parseInt(Integer.java:527) at stringequationfull.StringEquationFull.main(StringEquationFull.java:42) Java Result: 1
What am I doing wrong?