aspose file tools*
The moose likes Beginning Java and the fly likes If statement problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If statement problem" Watch "If statement problem" New topic
Author

If statement problem

Caleb Ash
Greenhorn

Joined: Dec 29, 2010
Posts: 5


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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Caleb Ash
Greenhorn

Joined: Dec 29, 2010
Posts: 5
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Dec 29, 2010
Posts: 5
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If statement problem