Hi I have a code that when you input an expression it will store to an array but my problem is how can i put * between two variables when the input expression is like ab +c?it says null value.
here’s my code:
stack = strexp.toCharArray();
for (int k = 0; k < stack.length; k++) {
if (Character.isLetter(stack[k]) && Character.isLetter(stack[k+1])){
temp[k] = stack[k];
temp[k+1] = '*';
temp[k+2] = stack[k+1];
}
}
You should receive an
ArrayIndexOutOfBoundsexception, because you incrementkuntil it is equal to the last index in thestackarray and then you try to accessstack[k+1].Your loop expression has to be
The cause of the
NullPointerExceptionis not directly visible but I believe that you haven’t initialized thetemparray. Most likely because you do not know it’s exact size.I’d store the result in a
listStringBuilder instead: