In the main class the user enters text trying to find the file in the same directory as the source file.
The string gets passed into the constructer and a file object is made but the file isn't found.

The print statements are just to find out what is happening.
The commented out code makes no difference either way.

Generateresponse(String humanphrase){
        //humanphrase += ".txt";
        System.out.println("in new class");

        //checks if file exists
        File f = new File(humanphrase);
        if(f.isFile()){
            System.out.println("exists");

Recommended Answers

All 12 Replies

hai yup790 i couldnt understand properly

can you explain what your requirement is ?

so that we will hep you

if you are looking for if the file exists or not
go with the following method available in the File Class

check it with 

if (f.exists())

let me know whether this is your requirement or not
happy coding

commented: since this is a correct answer to the question, see no reason why it should be flagged as a "Bad Post" +14

The string gets passed into the constructer and a file object is made but the file isn't found.

If you think the file should be found but it isn't being found then you should print out f.getAbsolutePath() which should tell you where the file is supposed to be. Then there should be no doubt about whether is should be found and why it's not being found.

commented: rep++ ;-) +13

bguild: the solution provided by radhakrishna.p does exactly what the OP is asking for, no need to look for "deeper truths" here.

Testing for f.exists() seems unlikely to work if f.isFile() does not. They do exactly the same thing unless f is a directory or otherwise not a normal file, and I'll be surprised if the problem turns out to be that the file was a directory.

yet possible. a directory can also be seen as a "file" so, isFile is not really what you want.

The isDirectory() method makes it very clear that the File class doesn't see a directory as a file. If in doubt read the API...

public boolean isFile()
...
Returns:
true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise

yes, but I wasn't referring to what the api's say, I was rather talking about how computer users in non-java language talk about it.
but still, the .exists method should do the trick.

.exists down't work
The commented code doesn't work
Would it work to write a copy of the exists method in the class

this is the code for the method that calls that class

String Human_question;      
Scanner s = new Scanner(System.in);
Human_question = s.nextLine();
System.out.println(Human_question);
Generateresponse gr = new Generateresponse(Human_question);

.exists down't work

.exists does work. It's been in the Java API forever, and is used in more programs than we can count. The fault almost certainly is either in your understanding or in your code. The most likely problem is that Java is looking for the file in a different directory. Follow bguild's excellent advice and print f.getAbsolutePath() to see where Java is looking for your file.

I love you.

Thank you so so so so much!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.