Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple I/O Java Error

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Thanked 0 Times in 0 Posts

    Default Simple I/O Java Error

    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.,

    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++ ) {

    The file I ma reading the ints from looks like this. (named math.txt)


    Help would be awesome. Thanks.
    Last edited by Prox; October 25th, 2011 at 11:02 AM.

  2. #2
    Join Date
    Oct 2011
    Thanked 2 Times in 2 Posts

    Default Re: Simple I/O Java Error

    I think you are wrong in this line:
    array[i] = Integer.parseInt(k);

    "*,/,+,-" may could not be parsered into an int.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Thanked 0 Times in 0 Posts

    Default Re: Simple I/O Java Error

    So then how would go about parsing the operators when you are reading the files from an input file?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    My Mood
    Thanked 167 Times in 158 Posts

    Default Re: Simple I/O Java Error

    You do not need to parse the operators. You can leave them as a String or use charAt to get then as a char.
    Improving the world one idiot at a time!

Similar Threads

  1. [SOLVED] Simple error that I can't figure out how to fix
    By javapenguin in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 11th, 2011, 07:27 AM
  2. Error for a simple array
    By DudeJericho in forum Collections and Generics
    Replies: 4
    Last Post: April 25th, 2011, 02:46 PM
  3. Simple code error
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2010, 03:25 PM
  4. Simple error can't figure out.
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 30th, 2010, 12:19 PM
  5. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM