xafizzle Posted March 1, 2014 Share Posted March 1, 2014 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: 56 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. Quote Link to comment Share on other sites More sharing options...
Short Posted March 1, 2014 Share Posted March 1, 2014 (edited) You could use a loop to check if it's at end of file, but I don't know java. while (get input) { do some stuff } Edited March 1, 2014 by Short Quote Link to comment Share on other sites More sharing options...
xafizzle Posted March 1, 2014 Author Share Posted March 1, 2014 No cause then it runs into that same problem. As far as I know a try block is the only way to get around it but I just don't understand why you can't catch that exception Quote Link to comment Share on other sites More sharing options...
Guest The_Monkey Posted March 2, 2014 Share Posted March 2, 2014 or you could make a nice gui... 1 Quote Link to comment Share on other sites More sharing options...
xafizzle Posted March 2, 2014 Author Share Posted March 2, 2014 The assignment was to read from a file, not from individual user input. Quote Link to comment Share on other sites More sharing options...
Papa John Posted March 3, 2014 Share Posted March 3, 2014 http://stackoverflow.com/questions/13927326/reading-input-till-eof-in-java 3 Quote Link to comment Share on other sites More sharing options...
xafizzle Posted March 3, 2014 Author Share Posted March 3, 2014 (edited) 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; } } } 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 March 3, 2014 by xafizzle Quote Link to comment Share on other sites More sharing options...
captaincracker Posted March 12, 2014 Share Posted March 12, 2014 If you send me your code I can finish it and send it back, I always like practice Quote Link to comment Share on other sites More sharing options...
Guest The_Monkey Posted March 13, 2014 Share Posted March 13, 2014 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. Quote Link to comment Share on other sites More sharing options...
captaincracker Posted March 13, 2014 Share Posted March 13, 2014 If it is for school, please don't do this. I just figured he was making it for himself Quote Link to comment Share on other sites More sharing options...
xafizzle Posted March 14, 2014 Author Share Posted March 14, 2014 I'm not asking for it to be done for me. I actually finished it. I'll give a pastebin link when I get back. I just felt like it was really inefficient. And I don't understand why I can't catch that exception. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.