Im supposed to write this java program, and im having a problem, and I cannot figure out why I am getting an Unhandled Exception error.
I put a comment at the error line.
PrintWriter writer = new PrintWriter (writingfile); (Line where I am getting uNhandled exception error.)
Write a program that reads numeric operations from a
file and writes their result into another file.
Numbers & results are integer.
• Operators (+ - * / ) have equal precedence.
• Input file
• Name: “input.txt”
• There can be many operations, each on its own line, e.g.,
2 + 3 * 10 / 2
10 / 3 * 2
• Output file
• Name: “output.txt”
• The result of each operation is written in one line, e.g.,
25
Code ~
import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; //There is NO PRECEDENCE in the math from the file public class Operations { public static void main (String [] args) { ArrayList<String> inputs = new ArrayList<String>(); int [] array = new int[10]; //change this once the add to method works. String k = ""; try { File inputfile = new File("math.txt"); Scanner in = new Scanner (inputfile); while (in.hasNextLine()) { inputs.add (in.nextLine() ); } in.close(); //all objects are inside of the ArrayList String type for (int i = 0; i < inputs.size(); i++) { //attempting to change from String to int k = inputs.get(i); array[i] = Integer.parseInt(k); } } catch (IOException e) { System.out.println("You done fucked up real good."); } File writingfile = new File ("output.txt"); //Writing to the file PrintWriter writer = new PrintWriter (writingfile); for (int i =0; i < array.length; i++ ) { writer.println(array[i]); } writer.close(); } }
The file I ma reading the ints from looks like this. (named math.txt)
1+2*3/3
1+2+3+4*3/3
1+2+4
Help would be awesome. Thanks.