I need a program that reads prefix expression and return its value. the expressi
ID: 3653612 • Letter: I
Question
I need a program that reads prefix expression and return its value. the expression only contains +,* and single digits. For instance: + 5 * 6 7 returns 47, in other words it multiplies 6 * 7 first then adds 5.
See my program below, I am having two errors that I want fix, PLEASE FIX THIS PROGRAM, DO NOT COPY AND PASTE A PROGRAM FROM SOMEWHERE THAT DO NO APPLY TO THIS PROGRAM. THANK YOU.
Ashley:
my program below:
import java.util.Scanner;
public class lab8
{
public static void main (String[] args)
{
char[] a;
a = new char[6];
a[0] = '0';
a[1] = '6';
a[2]= '*';
a[3]= '7';
a[4]='+';
a[5]= '5';
System.out.print(prefixcalc(a,0));
}
public static int prefixcalc(char [] a, int i)
{
if ((int)a[i]>=48&&(int)a[i]<=57)
return (a[i]-'0'); //or `(int)a[i]-48;
{
if ((int)a[i] == ('+'))
return (5+prefixcalc(a[i],i+2));
{
if ((int)a[i] == ('*'))
return (6*prefixcalc(a[i],i+2));
else
return 7;
}
}
}
}
my errors:
2 errors found:
File: S:us210lab8.java [line: 23]
Error: method prefixcalc in class lab7 cannot be applied to given types;
required: char[],int
found: char,int
reason: actual argument char cannot be converted to char[] by method invocation conversion
File: S:csi310lab7.java [line: 26]
Error: method prefixcalc in class lab7 cannot be applied to given types;
required: char[],int
found: char,int
reason: actual argument char cannot be converted to char[] by method invocation conversion
Explanation / Answer
The problem is you are passing a char instead of char [] In two places return (5+prefixcalc(a[i],i+2)); and return (6*prefixcalc(a[i],i+2)); a[i] is a char instead you should pass a return (5+prefixcalc(a,i+2)); return (6*prefixcalc(a,i+2));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.