Ask the user for prefix expressions and evaluate each expression. If the result of an expression is zero, then end. i have this code so far: import java.util.*; class QueueUtils{ static Boolean Operand(char c){ if(c>=48 && c<=57) return true; else return false; } static double Prefix_evaluation(String exp){ Stack Stack = new Stack(); for(int j=exp.length()-1;j>=0;j--){ if(Operand(exp.charAt(j))) Stack.push((double)(exp.charAt(j)-48)); else{ if(exp.charAt(j)==' '){ } else{ double o1 = Stack.peek(); Stack.pop(); double o2 = Stack.peek(); Stack.pop(); switch(exp.charAt(j)){ case '+': Stack.push(o1 + o2); break; case '-': Stack.push(o1 - o2); break; case '*': Stack.push(o1 * o2); break; case '/': Stack.push(o1 / o2); break; case '%': Stack.push(o1%o2); break; } } } } return Stack.peek(); } public static void main(String[] args){ Scanner sc= new Scanner(System.in); String exp; int flag; flag=0; double d; do{ System.out.println("Enter an expression in prefix form (operator comes first)"); exp = sc.nextLine(); d = (Prefix_evaluation(exp)); System.out.println(d); if (d==0){ System.out.println("Exiting"); flag=1; } }while(flag!=1); } } however, it is not providing the correct ouput that i need it is supposed to be: Enter an expression in prefix form (operator comes first)\n 12ENTER 12\n Enter an expression in prefix form (operator comes first)\n + 2 33ENTER 35\n Enter an expression in prefix form (operator comes first)\n + 5 8ENTER 13\n Enter an expression in prefix form (operator comes first)\n - * 17 5 + 4 6ENTER 75\n Enter an expression in prefix form (operator comes first)\n - 5 5ENTER 0\n Exiting\n but it is outputting Enter an expression in prefix form (operator comes first)\n 12ENTER 1.0\n Enter an expression in prefix form (operator comes first)\n + 2 33ENTER 5.0\n Enter an expression in prefix form (operator comes first)\n + 5 8ENTER 13.0\n Enter an expression in prefix form (operator comes first)\n - * 17 5 + 4 6ENTER 2.0\n Enter an expression in prefix form (operator comes first)\n - 5 5ENTER 0.0\n Exiting\n can an expert help fix my code so it provides the correct ouput

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Ask the user for prefix expressions and evaluate each expression. If the result of an expression is zero, then end.

i have this code so far:


import java.util.*;

class QueueUtils{
static Boolean Operand(char c){

if(c>=48 && c<=57)
return true;
else
return false;

}
static double Prefix_evaluation(String exp){

Stack<Double> Stack = new Stack<Double>();
for(int j=exp.length()-1;j>=0;j--){

if(Operand(exp.charAt(j)))

Stack.push((double)(exp.charAt(j)-48));

else{


if(exp.charAt(j)==' '){


}
else{
double o1 = Stack.peek();
Stack.pop();
double o2 = Stack.peek();
Stack.pop();



switch(exp.charAt(j)){

case '+':
Stack.push(o1 + o2);
break;
case '-':
Stack.push(o1 - o2);
break;
case '*':
Stack.push(o1 * o2);
break;
case '/':
Stack.push(o1 / o2);
break;
case '%':
Stack.push(o1%o2);
break;



}
}
}
}


return Stack.peek();
}


public static void main(String[] args){
Scanner sc= new Scanner(System.in);

String exp;
int flag;
flag=0;
double d;
do{
System.out.println("Enter an expression in prefix form (operator comes first)");
exp = sc.nextLine();
d = (Prefix_evaluation(exp));
System.out.println(d);
if (d==0){
System.out.println("Exiting");
flag=1;
}
}while(flag!=1);
}

}

however, it is not providing the correct ouput that i need

it is supposed to be:

Enter an expression in prefix form (operator comes first)\n
12ENTER
12\n
Enter an expression in prefix form (operator comes first)\n
+ 2 33ENTER
35\n
Enter an expression in prefix form (operator comes first)\n
+ 5 8ENTER
13\n
Enter an expression in prefix form (operator comes first)\n
- * 17 5 + 4 6ENTER
75\n
Enter an expression in prefix form (operator comes first)\n
- 5 5ENTER
0\n
Exiting\n

but it is outputting

Enter an expression in prefix form (operator comes first)\n
12ENTER
1.0\n
Enter an expression in prefix form (operator comes first)\n
+ 2 33ENTER
5.0\n
Enter an expression in prefix form (operator comes first)\n
+ 5 8ENTER
13.0\n
Enter an expression in prefix form (operator comes first)\n
- * 17 5 + 4 6ENTER
2.0\n
Enter an expression in prefix form (operator comes first)\n
- 5 5ENTER
0.0\n
Exiting\n

can an expert help fix my code so it provides the correct ouput 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education