• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object in a file

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

Any idea?
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zenitis,

Welcome to Java Ranch.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us more details; you don't say what you expect to happen, nor what actually happens.

You don't appear to be reading from any files anywhere. You appear to have both a Scanner and a BufferedReader reading from System,in; I am sure you don't need both.
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are trying to go too fast.You need to go very slowly.

Write down the stages of what you need to do. Then subdivide each stage into smaller stages, and subdivide those stages further . . .
Go through your main method and write down what each line does.
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what happens when you enter a name?

You are still trying too many things at the same time. Small pieces, then put them together.
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look through the constructors for the file writer classes; they often have a parameter which allows appending to the file.
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
at ofthalmiatreio.Asthenhs.write_new_record(Asthenhs.java:223)
at ofthalmiatreio.Asthenhs.main(Asthenhs.java:335)
Java Result: 1
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the code is fistly
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the find method it use the integer key (for client's id) and return an object
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
methods for opening a file close a file read file and readopen file
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and the last one for writing objects into file firstly it try to find if in the file exist object with same account else write the object in file
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


of course the main method..
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess that the problem is in write_new_record method especially in the try-catch. I don t exactly understand which exceptions i must use in this situation of files.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any possibility that BufferedWriter outputt;may be the problem?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;

Formatter out1;
FileWriter out;
Scanner input=new Scanner(System.in);
BufferedWriter outputt;

and i changed in the method open_FIle from output to outputt.
After that the program run and show me the message The information is stored.
Nevertheless none of the variables stored in the file.
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why dont you create serializable object (implements Serializable) and then write whole object in to the file?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Zenitis Stoichkov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
man thanks a lot for you help i ll try it right now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic