Hello to everybody, I m new to java so i ve got some problems with the new project that i have. I want to parse some variables to a txt file. I ve found some ways to do that by none of them worked properly. For example i ve got one class, some private variables and in the main method i tried to parse them to the file:
The code is:
Ok my problem that i want to parse in file more than one object. But in the file only one object is saved the last one. Sorry but i m new to java and i don t exactly understand some things. This is the reason that i tried both a Scanner and a BufferedReader reading.
Generally i want to make a programme for a dentist. Something like a calendar and i want to make a menu in which dentist can store his clients. So in this example i tried to store to file some informations about clients name surname ID...I first use the BufferedReader to parse something to variables and afterthat i tried to store it in a file.
the problem is that when i rtied to parse more than one object(a run of five variables) in a file only the last object is saved in the txt. I ll make a new method for bufferedReader in class if this is better..
Ok after 2 days searching and searching i found some things that may work.I first split the program in 6methods(plus set/get and constructors). When i tried the program the first time it was ok and the first object is successfully stored in txt. But after that i was receiving this:
Exception in thread "main" java.lang.NullPointerException
Java Result: 1
It is not possible to tell from the code posted where the Exception occurred; you will have to work that out for yourself. Go through the line and look at every object reference and work out whether it could be null. If you can't tell, put a System.err.printf command before that line:That will help you work out which object is null.
If it is the first line where you use that Writer, yes, it might be null. The code quoted doesn't show where you instantiate it. In fact Writers should be opened as local variables, and closed in a finally block, to release access to the File. Brian Legg gave a little example in this thread. You will have to look at older examples quoted from that thread and see where I made a mistake in a post to work out the whole thing. If you have difficulty following those old threads, I shall give an example later.
okay I got confused with Formatter bufferedWriter and FileWriter. I filnally change variables from class
# FileWriter out;
# Formatter out1;
# BufferedWriter output; //it s useless
# Scanner input=new Scanner(System.in);
# BufferedWriter outputt;
As promised yesterdayThat is how you can write an array of Strings to a text file, each on one line. The FileWriter class writes text into files. You will have to alter your logic for your application. It might be better to create a Patient object and give it a good toString() method, then you can useAs you probably already have realised, it is often easier to use a Formatter, but I have never worked out whether you can append anything to a file with a Formatter.