Issue
I'm creating a postfix calculator. I've read elsewhere and the issues usually are because of the Scanner. So, maybe that will be a head start for you? I have a Calculator.java
class and a CalculatorTester.java
class below.
Code:
public class Calculator {
private int x;
private int y;
public static void main (String[] args) {
}
public int postfix(String string){
Scanner scanner = new Scanner(string);
LLStack<Integer> stack = new LLStack<Integer>();
while (scanner.hasNext()){
if (scanner.hasNextInt()){
stack.push(scanner.nextInt());
} else {
if (scanner.next().equals("+")){
y = stack.pop();
x = stack.pop();
stack.pop();
stack.push(x+y);
}
if (scanner.next().equals("-")){
y = stack.pop();
x = stack.pop();
stack.pop();
stack.push(x-y);
}
if (scanner.next().equals("*")){
y = stack.pop();
x = stack.pop();
stack.pop();
stack.push(x*y);
}
if (scanner.next().equals("/")){
y = stack.pop();
x = stack.pop();
stack.pop();
stack.push(x/y);
}
if (scanner.next().equals("%")){
y = stack.pop();
x = stack.pop();
stack.pop();
stack.push(x%y);
}
}
}
return stack.pop();
}
}
public class Calculator {
private int x;
private int y;
public static void main (String[] args) {
}
public int postfix(String string){
Scanner scanner = new Scanner(string);
LLStack<Integer> stack = new LLStack<Integer>();
while (scanner.hasNext()){
if (scanner.hasNextInt()){
stack.push(scanner.nextInt());
} else {
if (scanner.next().equals("+")){
y = stack.pop();
x = stack.pop();
stack.pop();
stack.push(x+y);
}
if (scanner.next().equals("-")){
y = stack.pop();
x = stack.pop();
stack.pop();
stack.push(x-y);
}
if (scanner.next().equals("*")){
y = stack.pop();
x = stack.pop();
stack.pop();
stack.push(x*y);
}
if (scanner.next().equals("/")){
y = stack.pop();
x = stack.pop();
stack.pop();
stack.push(x/y);
}
if (scanner.next().equals("%")){
y = stack.pop();
x = stack.pop();
stack.pop();
stack.push(x%y);
}
}
}
return stack.pop();
}
}
Terminal output:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Calculator.postfix(Calculator.java:23)
at CalculatorTester.main(CalculatorTester.java:4)
Solution
I'm not sure about how really Scanner class works. But, what I know is that Scanner read line per line, and not character by character (I'm not sure if there is a way to read like that). So, you first mistake is you are reading a String in one line, so, when you do this:
scanner.next()
The scanner really returns your full string "345*+"
. You can fix this, using \n
in each character:
calc.postfix("3\n4\n5\n*\n+");
Now, inside your postfix method, you have a mistake about reading with scanner:
else {
if (scanner.next().equals("+")){}
if (scanner.next().equals("-")){}
....
}
If you see, you are using inside the if conditions scanner.next()
. When you use that method, you are moving the pointer of the Scanner to the next input.
For example, you have the next input: "Hello\nWorld\nFuzz"
When you use scanner.next()
the first time, the pointer will be in "Hello"
.
The second time you use it, will be in "World"
and finally when read "Fuzz"
and you use scanner.next()
the pointer will be in the EOF(end of file).
Then, the scanner.hasNext()
method will return false.
To fix that mistake, you have to assign what scanner.next()
returns to a String variable, and then, use that one for comparing.
Your code must be like this:
private int x;
private int y;
public static void main (String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.postfix("3\n4\n5\n*\n+"));
}
public int postfix(String string){
Scanner scanner = new Scanner(string);
Stack<Integer> stack = new Stack<Integer>();
while (scanner.hasNext()){
if (scanner.hasNextInt())
{
stack.push(scanner.nextInt());
} else {
String next=scanner.next();
if (next.equals("+")){
y = stack.pop();
x = stack.pop();
stack.push(x+y);
}
if (next.equals("-")){
y = stack.pop();
x = stack.pop();
stack.push(x-y);
}
if (next.equals("*")){
y = stack.pop();
x = stack.pop();
stack.push(x*y);
}
if (next.equals("/")){
y = stack.pop();
x = stack.pop();
stack.push(x/y);
}
if (next.equals("%")){
y = stack.pop();
x = stack.pop();
stack.push(x%y);
}
}
}
return stack.pop();
}
I had to remove the lines
stack.pop();
inside thepostfix()
method, because that was causing an EmptyStackException
Answered By - Edgar Magallon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.