-1

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322

2 Answers2

4

You are trying to parse an empty string as an int; hence the exception. Your logic for dealing with operators is wrong. Your code assumes that there is always a number between operators, but if you look at your equation, this is clearly not the case. (Look at occurrences of parentheses.) The exception is being raised because, as the message states, ns is the empty string.

You will need to develop a more general parsing logic. Search the web for "parse equation java" for lots of examples of how to do this. If you just want the ability to evaluate equations, take a look at this thread or this one.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
4

nsi = Integer.parseInt(ns); will throw:

NumberFormatException - if the string does not contain a parsable integer.

Debug your code and you'll see that ns doesn't contain a parsable integer. (Hint: it is empty)

Tip: The stack trace is very informative, you can easily know where the problem occurred and why did it occur. A simple investigation will lead you to the problem. In this case, it's an exception that was thrown from parseInt (Class Integer), you can see there a good documentation of the method. This will help you a lot to find out what is your problem.

Community
  • 1
  • 1
Maroun
  • 94,125
  • 30
  • 188
  • 241