0

The program is basically about reading from a text file, storing the current data into an ArrayList, then writing data (from user input) into the same text file. Kindly let me know where I am going wrong in this sub-part? The data inside the text file is as follows:

abc t1 1900

xyz t2 1700

The compiler is showing an error at the line output.format("%s%s%s%n",

public class justTesting {
    private Scanner input;
    private Formatter output;
    private ArrayList<Student> tk = new ArrayList<Student>();

    public static void main(String[] args) {
        justTesting app = new justTesting();

        app.create();
        app.writeToFile();
    }

    public void create() {
        Text entry = new Text();
        Scanner input = new Scanner(System.in);

        System.out.printf("%s\n", "Please enter your name, ID, and year: ");

        while (input.hasNext()) {
            try {
                entry.setName(input.next());
                entry.setTelNumber(input.next());
                entry.setDOB(input.next());

                for (int i = 0; i < tk.size(); i++) {
                    output.format("%s%s%s%n", tk.get(i).getName(), tk.get(i)
                            .getTelNumber(), tk.get(i).getDOB());
                }

            } catch (FormatterClosedException fce) {
                System.err.println("Error writing to file.");
                return;
            } catch (NoSuchElementException nsee) {
                System.err.println("Invalid input. Try again: ");
                input.nextLine();
            }
            System.out.printf("%s\n", "Please enter your name, ID, and year: ");
        }
    }

    public void writeToFile() {
        try {
            output = new Formatter("testing.txt");
        } catch (SecurityException se) {
            System.err
                    .println("You do not have write access permission to this file.");
            System.exit(1);
        } catch (FileNotFoundException fnfe) {
            System.err.println("Error opening or creating file.");
            System.exit(1);
        }
    }
}
13
  • 4
    What was output declared as? Is it even declared/instantiated? I can't see that with this snippet of code. Commented Jul 11, 2012 at 19:13
  • What does the error say? Commented Jul 11, 2012 at 19:14
  • 2
    You're stating 4 references in the format %s%s%s%n and you only pass 3. Commented Jul 11, 2012 at 19:15
  • output is declared as private Formatter output and the error says Exception in thread "main" java.lang.NullPointerException Commented Jul 11, 2012 at 19:17
  • @m0skit0 The %n is for carriage return in a text file. It works like /r/n Commented Jul 11, 2012 at 19:19

1 Answer 1

1

I removed %n and it worked. Thanks everyone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.