Whats wrong with my code? It throws an array index out of bounds exception? - input stack size 5000 latex
Input string;
Scanner console = System.in (new);
Stack stack = new Stack ();
System.out.println ( "Enter a mathematical expression:");
console.nextLine entry = ();
StringTokenizer st = new input (StringTokenizer, "");
char [] token = st.nextToken (). toString (). toCharArray ();
int count = 0;
int sum = 0;
this int i = 0;
int prod = 0;
quo int i = 0;
while (st.hasMoreTokens ()) (
if (Character.isDigit (tokens [count])) (/ / throw an exception on this route
stack.push (tokens [count]);
count + +;
)
else (
if (st.nextToken (). ("+")) equals (
sum = Integer.parseInt (stack.Pop (). toString ()) + Integer.parseInt (stack.Pop (). toString ()) ...
stack.push (sum);
)
else if (st.nextToken (). equals ("-")) (
diff = Integer.parseInt (stack.Pop (). toString ()) + Integer.parseInt (stack.Pop (). toString ()) ...
stack.push (diff);
)
else if (st.nextToken (). ("*")) equals (
prod = Integer.parseInt (stack.Pop (). toString ()) + Integer.parseInt (stack.Pop (). toString ()) ...
stack.push (total);
)
else if (st.nextToken (). ("/")) equals (
quo = Integer.parseInt (stack.Pop (). toString ()) + Integer.parseInt (stack.Pop (). toString ()) ...
stack.push (quo);
)
else if (stack.size () == 1) (
System.out.println (stack.Pop ());
)
)
)
2 comments:
I think it is on this train wreck
char [] token = st.nextToken (). toString (). toCharArray ();
I suggest you forget the char [] and use only the mark that has already been created, and just browse through the various tabs.
Which line? "
Perhaps their approach is to try to make a pop an empty stack pop ... must be provided to ensure that your battery is not empty ...
But whatever ... You try something on the 1 do-element ... do not fly it
Otherwise ... trying to debug .. IT WAY easier to teach "soft skills"
Post a Comment