• Post Reply Bookmark Topic Watch Topic
  • New Topic

Check if an if statement returned true  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm checking using an if statement if a file exists like so:



If it returns true for existing then how would I do another if statement that would say if file returned true then check said file for an id. If Id exists extract all data associated with that id and store in Vector object. Would it be like this:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:If it returns true for existing then how would I do another if statement that would say if file returned true then check said file for an id. If Id exists extract all data associated with that id and store in Vector object....

Whoa hoss. Let's back up a bit here.

You have a method defined as:
public boolean export(String envName, String fromFile, String toFile) { ...

First up: What is it supposed to be doing?

Second: What is that boolean it returns supposed to represent? Success/failure? Or something else? Right now, all it says is whether the file exists or not, which doesn't seem to have much to do with an "export" - but I could be wrong.

I think we need a bit more background before we can advise properly.

However, before any of that:
if(file.exists() && file !=null)
is definitely wrong. Think about it: What if file does == null? Have a thunk how you might put that right.

Winston
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a boolean because I was returning true or false. This class is to extract certain information from xml file and place that data in a new xml file which is why I have fromFile and toFile

If returns true it has been successful and found the file if not it returns false.

This is what I am trying to do:

  • I made a check to see if the file existed or not
  • Then I'd check inside the file for specific id
  • If that existed I'd then get all the information from the xml file the id is related to and stick it in another xml file


  •  
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    An if statement never returns true. It never returns anything. An expression returns a value and a method can return a value, but statements don't return things.

    Your code looks over complicated, and as you already know you cannot have code after a return statement. Do you mean you want to keep going until you have a valid File object which actually exists? The method you have shown us will not compile because you are returning halfway through it.
    Do you know the newer ways to get your hands on a file? If not you might do well to go to the Java Tutorials. They call File legacy code. If you are going to use File if would be something like this
    Remember that snippet might throw a checked FileNotFoundException. Do you know how to find files with a JFileChooser? Is there any chance of f being null there?

    You appear to have several things to do: you want to find a source file, you want to find a destination file and you want to do some reading and writing. I make that four things. You ought not to do four things in a method. You ought to do one thing in a method. So that makes five methods. One for each of those four things and one to put the whole lot together. Give or take the odd dozen.
     
    Alice Hampton
    Ranch Hand
    Posts: 217
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have a version that uses JFileChooser fine but I'm not allowed to use it.I have to do everything without an GUI elements. I'm still using Java 1,6 so I will have to look to see if it would work if it's s newer way. Yeah I ought to split them up in to different methods. But still, is there a check that can be done to see if the file check came back as it being in existence?
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alice Hampton wrote:It's a boolean because I was returning true or false.

    OK. Not a very good answer, but we'll let that pass.

    This class is to extract certain information from xml file and place that data in a new xml file which is why I have fromFile and toFile

    Seems reasonable. What's that envName all about?

    If returns true it has been successful and found the file if not it returns false.

    Ah. And that's where you have a problem. "been successful and found the file" has FOUR possible outcomes:
  • successful & found file
  • successful & didn't find file (???)
  • unsuccessful, but found file
  • unsuccessful & didn't find file (???)
  • How do you propose to cater for all of them with a boolean?

    This is what I am trying to do:
  • I made a check to see if the file existed or not
  • Then I'd check inside the file for specific id
  • If that existed I'd then get all the information from the xml file the id is related to and stick it in another xml file

  • So presumably that means that if ANY of those steps failed, you want to return false; otherwise, you want to return true. Right?

    That makes it a "success/failure" value then, which is fine; but if it returns false, you still don't know WHY it failed.

    Sorry if I'm sounding pedantic, but its quite important. A boolean has only TWO values. Now if that's all you need (ie, you only need to know if the method was successful or not) then that's fine.

    An int, on the other hand, has millions, so you could return 0 if successful, and something specific (and documented) if it doesn't. Eg:
    1 = File didn't exist.
    2 = File exists but didn't contain ID
    3 = File exists, contains ID, but something else happened.

    Even better might be to create an enum, eg:and return one of them. It tends to make code much more readable.

    However, getting back to your code, and dealing ONLY with the first condition - what do you think you might do to change it so that you can continue if the file DOES exist?

    Winston
     
    Paweł Baczyński
    Bartender
    Posts: 2083
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This conversation reminded me of ExtraBoolean
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote: . . . what do you think you might do to change it . . .
    Winston
    Does that mean without reading my latest post?
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Does that mean without reading my latest post?

    Of course not mon esteemed brave. It's just that you posted while I was composing my missive.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paweł Baczyński wrote:This conversation reminded me of ExtraBoolean

    Actually, not so weird as it sounds. I know quite a few progs that used to use the fact that a Boolean could be null...

    Winston
     
    Alice Hampton
    Ranch Hand
    Posts: 217
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    To be honest I would of made another method to look like that:



    And I would of checked to see if it had returned true and if it had then I'd of gone on to look to see if line contained within the file was the line I was after.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alice Hampton wrote:To be honest I would of made another method to look like that:

    Possibly - and I certainly think that more than one method may be a good idea - but I suspect you're overthinking things.

    How about this (in "Java-ish" pseudo-code):?

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alice Hampton wrote: . . . I'm still using Java 1,6 . . .
    I knew your version was old I presume you mean Java6. That means you can't use the newer Path API. If you go to the Java Tutorials link I posted, it says you can find an older version of the Java Tutorials here.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Slight digression while Alice is mulling things over; but I've never particularly liked methods that return booleans, because it seems to me that there's almost always a more useful value that could be returned.

    Classic example to me is List.contains() and List.indexOf(). If you have the latter, you don't need the first. And it often crops up with things likes dates too.

    I suspect it's because I generally have my "toolmaker" hat on, as opposed to my "business programmer" one.

    Winston
     
    Dave Tolls
    Ranch Foreman
    Posts: 3065
    37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Paweł Baczyński wrote:This conversation reminded me of ExtraBoolean

    Actually, not so weird as it sounds. I know quite a few progs that used to use the fact that a Boolean could be null...

    Winston


    tribool
     
    Alice Hampton
    Ranch Hand
    Posts: 217
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the link! I guess you remembered I had an old version from before ;)
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12564
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dave Tolls wrote:
    Winston Gutkowski wrote:
    Paweł Baczyński wrote:This conversation reminded me of ExtraBoolean

    Actually, not so weird as it sounds. I know quite a few progs that used to use the fact that a Boolean could be null...

    Winston


    tribool

    What is truth?
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:Slight digression . . . is List.contains() and List.indexOf(). If you have the latter, you don't need the first. . . .
    contains() reads something like this:
    Better than always having to write
    if (myList.indexOf(something) == -1) ...
    so I think I disagree there.

    I still think you need to break that into several methods. Each with its own error handling.
    By the way: How are you feeding the details of the files you are looking for? You aren't hard‑coding them, I hope. Feeding them by hand?
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:. . . I know quite a few progs that used to use the fact that a Boolean could be null...

    Winston
    If you use SQL, then every boolean can be NULL.
     
    Alice Hampton
    Ranch Hand
    Posts: 217
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm only ever going to be using the same xml file to be pulling from because it's that big. The only hardcoded thing is the actual path of the file I will be using :P.
    I am not using SQL for this
    I have broken it down now into five different methods so it's easier to see. I'm still lost about how to improve my original check to see if the file exists however :<
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12564
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The problem I see is this:



    Let's assume "file" is null. What happens?

    You first call the method file.exists() on a null reference. This is going to throw a NullPointerException. You will never even get to the second half of your if condition.

    at the VERY least, you should reverse the order:

     
    Alice Hampton
    Ranch Hand
    Posts: 217
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh right I see - that makes sense what you are saying - I should of seen that myself too as I understood straight away what you were going to say when I saw the code in the post. Thank you
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What chance is there of the File variable pointing to null?
    AH has said File f = new File(...);
    The f reference therefore has to point to a real object, or an Exception would have to be thrown. I think the null check is unnecessary.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12564
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote: I think the null check is unnecessary.

    That may be...I honestly didn't look at anything other than the one line I quoted.
     
    Alice Hampton
    Ranch Hand
    Posts: 217
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It keeps just returning false for finding the file :< What am I doing wrong :/ I have the file path specified correctly....
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But you and Winston are right to point out the null check must precede the .exists() call.

    You may also need to call two other methods on the File objects: 1 2
    Alice: please show us how far you have got with the reading.
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alice Hampton wrote: . . . What am I doing wrong . . . ....
    Don't know. Show us the file path (if you are allowed to) and the method to open that file. Just the source file at the moment. I presume that is the one you are having difficulty finding.
     
    Dave Tolls
    Ranch Foreman
    Posts: 3065
    37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    fred rosenberger wrote:
    Dave Tolls wrote:tribool

    What is truth?


    Ha...it was while laughing at that one that someone pointed the Boost tribool out to me, however many years ago now...ooh, 2005...
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have come across several different versions of three‑value booleans. We here have four boolean bunches we know about:
    true
    false
    null

    There are people like Cliff Jones who consider that it is valid to create a predicate like log(x) < 0 and say it is valid for all values of x whereas I would simply say non‑positive values are outwith the domain of the log relation so that predicate is invalid for those values.

    But let's get back to AH and her two‑valued logic.
     
    Alice Hampton
    Ranch Hand
    Posts: 217
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have a project called DM in which my java classes and the xml file is sitting in. I have my path


    That is my export.java code


    and this is my main:


     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Alice,

    now, do you mean that the boolean 'ok' is true, but that the S.o.println prints 'false'?

    Greetz,
    Piet
     
    Alice Hampton
    Ranch Hand
    Posts: 217
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yeah, it just prints false everytime and the folder path is 100% correct so it needs to be printing true
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is that the whole of your method? I thought you would have a method which found and returned the File reference.

    Why are you calling the export method twice? That ain't right. I need to go and cook tea now so haven't time to look farther, but it is quite possible the two calls are interfering with each other.
    And avoid System.exit.
     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Alice,

    well, I can't see any difference between the valuation of 'ok' and what S.o.println()
    should be doing. So, either there is some other code that you did not show, or else
    put some S.o.println() 's in your 'export' method. Print out the full input, and print out
    what 'file != null' gives, and what 'file.exists()' gives.
    Then, compare these prints when you determine 'ok', and when you do your S.o.println().

    And of course, if you use an IDE, a break within the 'export' method and a single stepper,
    running the debugger would be a great help as well.

    Greetz,
    Piet
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Simpler: add System.out.println(ok); at line 9. I think that will print true.
     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the statement was that ok and the S.o.println give different outcomes.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alice Hampton wrote:Yeah, it just prints false everytime and the folder path is 100% correct so it needs to be printing true

    First: For a proper test, your code should be identical for both cases.
    So, my advice would be to move the 'ok' assignment AFTER your field definitions on lines 10-12 and call the method EXACTLY the same way as you do in your println() statement.

    Second: Add a println() statement for ok immediately after its assignment.

    Third: Forget Java. Does 'Export.xml' actually exist?

    Fourth: If so, DELETE IT (or rename it). And run your test again. Hopefully, it should print false twice (assuming your code is exactly as you posted before). If not, come back, and show us what it looks like. The whole ExportEnvironment class (or at least its declaration and all its fields, and all the methods you're using).

    Fifth: If the last step worked, add (or rename) 'Export.xml' back, and run the test again. It should print true twice this time. If not, as before....

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I suspect the problem may be related to calling the same method twice.
    You call it once and open the File. Then the OS marks it as in use and locks it. Then you can't open the file again.
     
    Dave Tolls
    Ranch Foreman
    Posts: 3065
    37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:I suspect the problem may be related to calling the same method twice.
    You call it once and open the File. Then the OS marks it as in use and locks it. Then you can't open the file again.


    I don't see any code that opens the file.
    Just the creation of a File reference, which has no effect on the file system.

    But yes, if there's code that opens the file and doesn't close it (which we can't see) then that's a good candidate.
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Doesn't creating the File object open the file?
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!