By testing the code out, I hardcoded it for now to delete one item which is "tv" but it still remains in my text file.
my file consists:
Ps4
Xbox
tv
Iphone
Alienware_desktop
tv is not deleted and the tempFile was never deleted. Really nothing changed, only the addition of the tempFile.
There is no good examples out their on the internet so if anyone can help me out that would be great! The deleteItem coding is between lines 132 and 164...
George Avilez wrote:By testing the code out, I hardcoded it for now to delete one item which is "tv" but it still remains in my text file.
my file consists:
Ps4
Xbox
tv
Iphone
Alienware_desktop
tv is not deleted and the tempFile was never deleted. Really nothing changed, only the addition of the tempFile.
What is in the tempfile? Is the tv line removed from the temp file?
trimmedLine is exactly the same as currentLine, ie it's not trimmed. I suggest you change your code so it is trimmed as your file may contain spurious whitespace. You may also want to look at using equalsIgnoreCase() rather than equals unless you really need a case sensitive test.
Also check what the value of the successful variable is after you try to rename the file. You haven't closed the reader, so the rename may be failing because of that.
Alright I finally got it to somewhat work. 'tv' is deleted in the new tempFile but all the Strings are on one line. How do I keep it like it was in the gifts.txt? And none of the file names changed. Can anyone see what's wrong? DOes it have to do with the Boolean variable?
ou may also want to look at using equalsIgnoreCase() rather than equals unless you really need a case sensitive test.
That's actually something I wanted to ask also, I was going to have the user enter "_" instead of spaces (incase there gift was more than one word) because then the program will kinda be messed up when each println separates words. Example: xbox 360 will be separated when I need it on one line. So with that in mind, will ignore case not work since the underscore may be present?
ou may also want to look at using equalsIgnoreCase() rather than equals unless you really need a case sensitive test.
That's actually something I wanted to ask also, I was going to have the user enter "_" instead of spaces (incase there gift was more than one word) because then the program will kinda be messed up when each println separates words. Example: xbox 360 will be separated when I need it on one line. So with that in mind, will ignore case not work since the underscore may be present?
Why would each println separate words? println just prints the text you give it and then prints the platform specific newline char(s).
To answer your question equalsIgnoreCase will match underscores in both strings but won't match underscores against space chars.
George Avilez wrote:Now my only question is how do I properly rename the templFIle to "gifts" and deleting the old "gifts" file?
Call delete on the gifts file object and then pass the object to the tempFile object's rename() method. Remember to check the return values from these methods to ensure they have succeeded before continuing.