• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading from and writing to a file and then read the file again incl. data written

 
Roberto Mol
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey fellow Ranchers,

i'm having a bit of trouble with using the Scanner and the Printwriter. I start with a file like this (1 = amount of Houses in the file)

The user gets (let's say for simplicity) 3 options: 1. Add a House to the file, 2. Get all Houses which fullfil requirements (price, FOR SALE / SOLD etc.) and 3. Close the application.
This is how I start:

Then I have a switch for all of the three cases. I keep the scanner open, so Java can get the user input (house = for sale or sold, price = ... etc). If the user chose option 1, and all information needed is inputted and scanned, the House will be written to the file (which looks like what I typed above).
For this, I use try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Makelaar.txt", false)))). This works perfectly (at least so it seems.)
If the user chose option 1, and all requirements are inputted and scanned, the Houses will be read (scanner) from the file and outputted. For this I use the same Scanner sc. This also works perfectly (so it seems atleast).

My problem is as follows: If a House has been added, I can only read the House(s) which were already in the file. Let's say I have added 2 houses, and there were from the start 3 houses. If option 2 is chosen, the first 3 houses will be scanned perfectly. An exception will be caught for the remaining 2 (just added) Houses. How can I solve this? I tried to close the Scanner, and reopening it, but apparently Java doesn't agree with this

All help is appreciated. If more info is needed, I will gladly provide it!
 
Piet Souris
Rancher
Posts: 1632
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Roberto,

I'm not sure what the problem is. Have a look at the following code:

the output is (as expected)

So you see that I write and read two times in succession to the same file.

Can you show the code that you use to write out the House data and the code
that reads them in again?

Also, I don't think it is handy to store the number of houses in line 1.
That means that each time you add another house to the file, you somehow
must change line 1. But anyway, first things first.

Greetz,
Piet
 
Roberto Mol
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Piet,

first of all, thanks for your answer! About the number of houses on the first line: This is obligatory. This project is for a school assignment, and the number of houses must be on the first line. I do agree though :P

In my main, i have this written in a while loop. The While loop ends, if 3 is pressed. Also, in the main there is a switch statement, since there are 3 options, hencte the break.

My writeToFile function:

Lets just focus on the KoopWoning.writeToFile function (I said for simplicity 3 options, but actually I have 5 options; 2 sorts of Houses). My writeToFileK function:

My read function:

Lastly, my woning.read function:


Quite a wall of text, apologies for that. I deeply hope that you know what's the problem Thanks for your help
 
Piet Souris
Rancher
Posts: 1632
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Roberto,

we zouden natuurlijk Nederlands kunnen klessebessen, maar de voertaal hier is Engels...

I must say: the whole writing procedure looks a bit complicated to me, but basically,
I see nothing wrong.

The only thing that looks suspicious is your static Portefeuille read()' method.
In line 06 you read the number of 'woningen'. with the 'nextInt()' method.
Now, be aware that the next 'nextLine()' reads an empty line, namely the remaining part
of the first line. Have a look at line 09. Here I see 'sc.nextLine();'.
The first time this command is executerd, it reads the rest of the first line.

And for every other value of i in this loop, you also execute 'sc.nextLine()', i.e.
you read a line and do nothing with it.

As said, depending on the contents of your file, that may or may not be correct,
but I have my doubts.

So what I like to suggest is the following:

start with an empty file, add 1 'woning' to it and write it out.
Open the created file in say Wordpad or any other texteditor you like,
and check for the contents.

Next, read the file in, using your 'read' method, and after each read of
a line, print the line out. For instance, in line 11 of the 'read method,
do a 'System.out.println("tag is: " + tag);'
Do the same at line 12 and a half: print out the contents of 'wlijst'.

Then do the same, while adding more and more woningen, and reading them in.

That way, we can spot where things go wrong. So, a bit of work to do!

Greetz,
Piet
 
Roberto Mol
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

(ach Engels is ook goed :P) Thanks a lot for your time, and your super awesome tip. It turns out that the first three houses are read correctly. However, at the 4th Woning, the tag is not "VERKOCHT" or something like that. At the 4th Woning the tag is skipped meaning that tag = Street + Housenumber.
Does this make sense to you?

I'm trying to find where the error is. I sincerely hope you could help me find it though, cause I probably won't find it

However, thanks for all your effort up till now Beneath, the output


EDIT: So apparently, it's not about the 4th woning at all. Apparently if the last Woning is a rental house (HuurWoning, doesn't have energiepeil/energy level) then the exception is caught :O (So the Woning after the rental woning can't be read). Am trying to figure this out.
 
Roberto Mol
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet, thanks a lot for your help. Your (basic) tip to just print everything that was read, made me see what I did wrong.

Because, in the Woning.read(), I did if(sc.hasnext()), when I was reading a HuurWoning (without energiepeil) I read the tag to check. Therefore, when I was reading the Woning after a HuurWoning, I already had read the tag, so my tag became the street.

It's solved now, thank god, and thank you Piet!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!