Welcome to The Forum

Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads

Rpn Calculator Java


xafizzle
 Share

Recommended Posts

I'm making an RPN calculator that reads input from a file using the FileReader Scanner. It works using a .txt file that has numbers and operations entered on separate likes like such:

 

5

6

8

+

4

-

sqrt

 

So in that case it would add 8 to 6, subtract 4 from the sum of that, and take the square root of that answer. All of that works fine until the end of the file when there is no more information to parse. I get a NoSuchNumberException and java doesn't let you catch that exception for whatever reason. I got around that by using a try/finally but I want to know if there's any other way to do it that's more efficient. Also, an explanation as to why you can't catch that exception would be appreciated.

Link to comment
Share on other sites

It doesn't work that simply for me. Perhaps there's something I'm forgetting as I'm not coding it the same way but it doesn't work unless I include the hasMore method.

 

The code:

import java.util.Scanner;
import java.util.ArrayList;
import java.lang.Math;
import java.io.*;
public class StackedCalculator {
public static void main(String[] args)throws IOException{
 StackedCalculator calc = new StackedCalculator();
 calc.operations();
 Scanner scanner = new Scanner(new FileReader("myFile.txt"));
 String inputS = scanner.nextLine();
 ArrayList<Double> stack = new ArrayList<Double>();
	 while (inputS != null) {
		 if (calc.isNumeric(inputS)) {
			 calc.push(Double.parseDouble(inputS), stack);
			 System.out.println(calc.peek(stack));
			 /*if(!hasMore(scanner)) {
				 break;
			 }*/
			 inputS = scanner.nextLine();
		 }
		 else if (calc.isValid(inputS) && stack.size() > 0) {
			 calc.push(calc.calculate(inputS, stack), stack);
			 System.out.println(calc.peek(stack));
			 /*if(!hasMore(scanner)) {
				 break;
			 }*/
			 inputS = scanner.nextLine();
		 }
		 else if (!calc.isValid(inputS)) {
			 System.out.print("Not a valid operation");
			 inputS = scanner.nextLine();
		 }
	 }
}
public static boolean hasMore (Scanner scanner) {
 if (scanner.hasNext()) return true;
 else {
	 System.out.print("No more elements!");
	 return false;
 }
}
}

 

25fhh4w.png

 

1zqu0l4.png

 

And before I get shit for using stack methods improperly or inefficiently, the assignment was also to write my own stack methods using arrays.

Edited by xafizzle
Link to comment
Share on other sites

  • 2 weeks later...
Guest The_Monkey

If you send me your code I can finish it and send it back, I always like practice

 

If it is for school, please don't do this.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share