Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Question. (acting up)

Options
  • 23-05-2001 12:34am
    #1
    Registered Users Posts: 21,264 ✭✭✭✭


    this code...

    if (inputfile.exists())
    i == 1;
    else
    i == 2;


    Now if the filename is "s.txt" and the file isn't there then i = 2 but if the filename is "s" and the file isn't there then i = 1.

    Anyone know why?



Comments

  • Moderators, Education Moderators Posts: 1,863 Mod ✭✭✭✭Slaanesh


    Me being a newbie and all, but instead of i == 1 and i == 2 should it not be i = 1 and i = 2

    Slaan

    [This message has been edited by Slaanesh (edited 23-05-2001).]


  • Moderators, Social & Fun Moderators Posts: 10,501 Mod ✭✭✭✭ecksor


    What class is inputfile an instance of?


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Are you assigning a value to i before you call this, cos I can't understand why the double equals '==' is assigning a value to it. Perhaps we could see the code for the exists() method as well?


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    <font face="Verdana, Arial" size="2">Originally posted by Slaanesh:
    Me being a newbie and all, but instead of i == 1 and i == 2 should it not be i = 1 and i = 2

    Slaan

    [This message has been edited by Slaanesh (edited 23-05-2001).]
    </font>

    yea. whatever smile.gif it was late. That was just to point out what part was being executed.



  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    let me write it better.
    (snip)
       int i = 0;
    
    // both files "s" + "s.txt" don't exist.
    // Doesn't work    [b]String to_name = "s";[/b]
        String to_name = "s.txt"; //works
    
        File to_file = new File(to_name);
        if (to_file.exists())
            i = 1;  
        else
            i = 2;
    (snip)
    


  • Advertisement
  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    just a thought, but maybe try and pud some luvely curly braces aroud the if/else?

    i know an if clause doesnt need {} for one statement, but does it need braces if it is part of if/else?

    (try/catch need curly braces for just one line of code?)
    if ( to_file.exists() == true  )
    {
        i = 1;
    }
    else
    {
        i = 2;
    }
    
    


    i dont expect this to fix your problem, its the best i can think of. :)

    - Dead Bank Clerk -
    You must defeat my dragon
    punch to stand a chance.

    [This message has been edited by DeadBankClerk (edited 23-05-2001).]


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    time out...

    There is nothing wrong with the if statement.

    The problem is with exists(). It doesn't seem to behave and I was just wondering why.


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    another idea:

    why is the int i an int?
    // Declare a boolean flag 
    // for the file existing
    boolean fileExists = false;
    
    
    if ( to_file.exists() == true  ){
        fileExists = true;
    }
    else
    {
        fileExists = false;
    }
    


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    read my previous post. *sigh*

    Look if the code was...

    boolean fileExists = false;

    fileExists = to_file.exists();


    IT STILL WONT WORK PROPERLY. (btw, Bools are auto set to false anyway).

    [This message has been edited by Hobbes (edited 23-05-2001).]


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I used this and it worked for me, what package are you using with this?
    import java.io.*;
    public class hobbes
    {
    	public static void main(String[] args) 
    	{
    		int i = 0;
    		// both files "s" + "s.txt" don't exist.
    		// Doesn't work    String to_name = "s";    
    		String to_name = "C:\\s"; //works    
    		File to_file = new File(to_name);    
    		if (to_file.exists())       
    			i = 1;      
    		else        
    			i = 2;
    		System.out.println(i);
    	}
    }
    

    I'll add that it worked fine with
    String to_name = "s"; 
    



    [This message has been edited by Evil Phil (edited 23-05-2001).]


  • Advertisement
  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    jdk1.3.0_02 using java.io.*

    Yea that works on this machine. I'll test the code next time I'm in work. It's possible I have a contaminated jdk.

    cheers. smile.gif



  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Maybe you should try the full path in work as well. Probably makes no odds but worth ruling out anyway.


  • Registered Users Posts: 2,660 ✭✭✭Baz_


    Always wondered about that one meself, hmmmmm..

    Oh btw should it not be a single equals??

    What might be happening is that if you have any files starting with s in the directory you're working from then perhaps its using them by accident.

    //megoes off to check this


  • Registered Users Posts: 2,660 ✭✭✭Baz_


    just tried it in c and my explanation holds no water however java is a funny language, so I don't know...

    Anyways if its not that I don't think that I can come up with another guess.


  • Registered Users Posts: 932 ✭✭✭yossarin


    you could also try a relative path to the dir you're running in. java's funny about that...
    './s.txt'

    edit: just thought - is the class in a package ? 'cos then you'll have to provide a path from the root of the package.

    [This message has been edited by yossarin (edited 24-05-2001).]


Advertisement