This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
what I have here is a code that is basically writing 3 things onto their own lines, but it is checking to make sure that the file pin+".in" doesn't exist first, and if it does returns you to the start screen
and I can't figure out why the below happens
The file comes out looking like:
when it should look like:
It is that it will goto both of the if parts, the if, and then the else if aswell
and the final output looks like this (on the CMD Screen) when it should only be one or the other.
((This only happens if the file doesn't exist, if it does exist it works fine.)
File already exists. Please try again.
When it should be displaying
So think about what is happening. You have 3 pieces of information. When the first piece is entered !e is true, so the file is created and the first line gets created, written to, and closed. Then the second piece gets entered, and !e is false - the file exists because it was created when the first piece of information was entered. Same with the third piece.
P.S. You can avoid this:
And replace it with:
There is no reason to test both if (!e) and else if (e) - they are mutually exclusive, so as long as !e is false, then e must be true and you don't need the extra if statement.
Joined: Dec 29, 2010
This was the only way I could think of to check to see if it exists, so that the files wouldn't have more than needed, and so it wouldn't mess up if someone used the same int pin for a name
Is there and reason that you used file.exists() for the if statement, or was that ment to be f.exists()?
as the former gives an error that exists() is undefined for the type string.
and with your change in code (using f.exists() not file) still produces the same error, and I still get the error even if I switch the statements to look like yours
EDIT: actually with this code: all I get now is File already exists.