• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If statement problem

 
Caleb Ash
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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:
Caleb Ash

when it should look like:
Caleb Ash
718293456
50.25

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.)

Written
File already exists. Please try again.

When it should be displaying
Written
Written
Written

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Caleb Ash
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Caleb Ash wrote: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.

Nope. Sorry. That was my mistake for not copy/pasting and going from memory.

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



That code fix was in a post-script. It isn't a fix for your problem, it is a general coding concept. The fix to your problem was everything above the code.
 
Caleb Ash
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh okay, I see what I can do now

Instead of decalraing 3 seperate write statements;


Only declare one, and change what the addIn class takes
addIn(name, "\r\n" + spin + "\r\n", aamt, path);

Then it writes all 3 at once...
Thanks, and sorry for the above, guess I should really read instead of skim reading like I normally do,

Thank you
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of using \r\n; that will give a file format which is specific for certain operating systems, eg Windows®. I can't remember the exact name of the property, but you use something like this:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic