• Post Reply Bookmark Topic Watch Topic
  • New Topic

not printing everything in data file  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys I've been sitting for almost 45 minutes trying to figure out why I am getting this output,I am quite perplexed as I looked at previous code I wrote the day before and the code is identical yet that code works without a hitch prints out every item that was added to the map from my dat file,

I have a wrote to a .dat file I made three entries Adam,Bill and Jeff

for some reason even though the dat file contains three items 6 if you count the numbers,it only prints the name Jeff

and just to confirm that my map was not one in size it turns out the size is only one for some reason,

I am just perplexed as why this is the case my code yesterday works fine as I said,I'm just sitting looking blank at this code thinking what could be wrong everything looks right yet it does not print everything


the code is quite short,could anyone tell me why I am not getting the output I want? thanks




 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the error?

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there's no error,just it is only adding one item to the map when it should be adding three I have three names Adam,Bill and Jeff
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the Print class?
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you add your file 'file.dat'?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key that you added to Map must be unique.

Is it unique?





 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a print statement at line 23 that prints out the two values that were read so you can see what is being read.

Also add a call to printStackTrace to the catch block.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for some reason I can't copy and paste the dat file but I'll write down what is in it

(int),Adam,(int),Bill,(int),Jeff,

thanks
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Norman I added a print statement and it does do what I want it first adds Adam to the list then writes Jeff(prints them both when adding at line 23)

but for some reason when I go through the for loop it just prints the last value Jeff and when I try to print the size it says there is only one value in the map which is odd,is the map been overwritten for some reason,I don't understand how it would be though everything in the code looks right

thanks
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please copy and paste here what was printed at line 23 so we can see what was read and what was added to the Map.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana I think you just solved the mystery each key was not unique that was the problem thanks so much I forgot I'm not working with an ArrayList
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please confirm that you are dealing with a binary file, not a text file. InputStreams are for binary files and for text files use Readers.
Forget all about the close() method. Use try with resources instead. Reserve the exception handling for real things going wrong.
Why are you declaring that main () might throw an Exception? I can't see any checked Exceptions there. In lines 26‑34 you are catching exceptions but not apparently doing anything visible with them. Add a few printStackTrace() calls, so you will know whether an exception is thrown at all. Also put a println call in the loop, so you will know that you have actually read three names. Verify that the numbers are different from one another.
Remind us: do you have to look for an EOFException to confirm that you have reached the end of the file? I have always thought that was a very peculiar piece of design. Verify that you don't have to catch the EOF exception separately from the IO exception; I think you probably don't.
Remind yourself about files in the Java™ Tutorials.

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Norm and Dana =)
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Campbell I'll read up on that now,and yeah I have been told that try with resources is a much better way to do things since it was introduced in 7th version of the java sdk as far as I know
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very sure that your Ids was not unique.

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Dana I checked to make sure the keys were unique and turns out they actually were in the end

I'm still not sure why it's only printing one value as opposed to the three values I put in
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with a call to print the ID inside the loop. Verify that it actually runs thrice.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Campbell I tried what you said and now it only seems to be printing Adam and the ID of Adam

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I even wrote to the file again to make sure the file is full and checked it several times

this is one of those times where I'm just perplexed as to why the code isn't doing what I thought it would

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that the code that was used to write the .dat file?  The write and writeInt methods write different values to the file.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is probably no need to call flush() because the close() call will invoke flush() by itself. Have you confirmed that you are reading thrice?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Campbell I'm not too sure how I would do that,

and yes Norman

thanks
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:hey Campbell I'm not too sure how I would do that, . . .
Didn't I tell you? Put a print instruction in the loop which reads your file and verify that it prints three things. You will get nothing helpful from a Map in the writing method.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that and it seemed to only print one value which was Adam

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// just prints Adam

Strange the data1 doesn't show in the print out.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
data1  does indeed get printed but just once 16 and adam gets printed
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case you need something in the catch to find out why you have come out of the loop; add ex.printStackTrace(); in the catch, and probably print the Map too.. And move the creation of the input streams to try‑with‑resources so as not to confuse the catch; I think actually you ought to have done that already.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will do Campbell thanks

the reason I didn't move it to a try with resources block is I want to solve this problem first
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think having the close instruction in the catch will make the problem harder to solve.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this all goes back to what Norm said earlier:

Norm Radder wrote:The write and writeInt methods write different values to the file.


Form your writing code, the second set of data starts with an int that was written using the write method...
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys that was indeed the problem =)

I'm going to use try with resources from now on as Campbell suggested,

is there any downside to using try with resources over the normal try catch block method?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:. . . try with resources over the normal try catch block method?
A “normal” try‑catch isn't ideal for closing resources; you shou‍ld really use a finally, otherwise there is a risk of the reader not being closed. Try with resources obviates the need for the finally.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I've been first introduced to Random access Files today and it's giving em a little bit of trouble I created a string builder and I expected once I called the readFromFile function it would append the values in the file into the String the file random.txt contains a few words hi,hello,hey

but when I run the program I get no out put onto the console(when I print Stringbuilder)

anybody know why I get absolutely no output?

thanks

 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How was the file created? Did you use writeUTF() when you created it?

A random access file may contain text but is not a text file.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:. . . the file random.txt . . .
What random access file? If you have a text file, treat it as a text file. You will have to use a reader or a Scanner. You won't use any EOFs (I have already mentioned my opinion of EOFs), but you can test for the end of the file differently.
but when I run the program I get no out put onto the console(when I print Stringbuilder) . . .
You don't appear to be adding anything to the StringBuilder.

Go and look at the Java™ Tutorials again; I am sure I gave you a link earlier today. Remember:-
  • 1: A random access file is a kind of binary file. Use a random access file object for reading it.
  • 2: For binary files use input streams.
  • 3: For text files use readers; you can pass a reader to a Scanner's constructor.
  • 4: Reaching the end of a binary file is liable to cause an EOF exception to be thrown. You have to catch that.
  • 5: Reaching the end of a text file causes certain tests to evaluate to false and you can use that in a loop, etc.
  •  
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks guys I'll read over the tutorials again

    I didn't add to the stringBuilder but I wrote builder.append that didn't add to it?

     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hey Campbell I checked out the tutorial on the oracle page,I'm still not quite sure why the strings are not getting appended to the StringBuilder builder object

    thanks
     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I used a println to see if the string one is getting the inputted data from the file but it seems to have failed

    also I changed the file extension to a dat file and wrote new data to that file yet I still have no luck

     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!