• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boolean doesn't appear to work  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to read a xml file and delete it's contents. I do this by reading the file,writing it to a temp and then overwriting the original with the temp by renaming it



it does say the value of the local variable is not used however. I've debugged to ensure it hits the code it always prints out the line after too.It just does not overwrite my original xml file with the temp one. Does anyone know why? I thought it was going to work!

It's had votes on stack so I thought that would of been reputableStack - overwrite but the second one got voted as a good answer. But still I would like to know if my code can work or not
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

System.out.println("success");

Print out the boolean value that's returned by the method. Currently you are just printing a literal string and not the value of a variable.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I should of said I already did that - it just returns false. I tried doing it like this:



it stays true until I come to print it out :s

I also tried:
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Funny, I was about to post a similar question. This warning is given in eclipse when the Boolean only performes like a "getter" and not like a "setter"

Adding this code will show the difference:


In this latter case succesful is used to set the value succesful2.

So I believe this message is not hinting you in the proper direction in solving the real issue why it will not overwrite.

Maybe a redefined question in this forum will help.

GL

EDIT: initializing the boolean as true might even prove it for you more as I suspect the line "boolean successful = outputFile.renameTo(inputFile)" will apply a new value to succesful

And sorry as I don't know the solution to the real issue
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using that code you gave but I always just get false returned? which is not what I want to happen even if I make them true already they end up false.
Thank you for offering your time and writing some code however - I appreciate you doing that
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:I tried using that code you gave but I always just get false returned? which is not what I want to happen even if I make them true already they end up false.
Thank you for offering your time and writing some code however - I appreciate you doing that


No problem at all. BTW I was not attempting to solve your issue with this code. Just trying to point out that the bug is not in message: "The value of the local variable bestaat is not used"

So because of an error somewhere else "outputFile.renameTo(inputFile)" is returning false.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou for pointing that out Stijn - I have no idea what on earth could be causing the error - I've checked my file variables and I've debugged through and it all works fine up until like you said I get that message The value of the local variable successful is never used.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible that you haven't closed inputFile after reading from it.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've closed my file reader and buffered reader and file writer all before I have attempted to overwrite the file. This is driving me nuts why won't it delete anything !

I tried using mkyongs example to delete my xml file file and that doesn't work either - it will delete other xml files though so it must be my xml file thats the issue :3

*edit* If I rename the file to lowercase it deletes it - the problem is is is in upper and lowercase - anyone know a way round that?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice, please BeForthrightWhenCrossPostingToOtherSites <- that's a link, click it
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh sorry Darryl - I didn't think I was to do that with it being two completely different sites. My apologies i'll make sure to keep that in mind for future
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Alice, please BeForthrightWhenCrossPostingToOtherSites <- that's a link, click it


Alice wrote this in her first post:

It's had votes on stack so I thought that would of been reputable Stack - overwrite but the second one got voted as a good answer. But still I would like to know if my code can work or not


It's a little garbled, but the link is to StackOverflow.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:It's a little garbled, but the link is to StackOverflow.

... to a thread that dates from 2009.

Alice knows which other forum I'm referring to.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I know which forum you mean - I did update it with an apology and a link to here. To be fair mind I'm using java 6 so having a post from 2009 isn't all that bad :P
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!