Java doesnt locate text file

Viewed 34

So my code is really simple. I want to locate a txt file and if it doesnt exists the program should ask for the file name again

public static void main(String[] args) {
    System.out.println("Enter the file you want to check ");
    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();
    
    File file = new File(input);

    while(!file.exists()){

        System.out.println(input + " doesn't exist! Try again.");
        input = scanner.nextLine();
       
        file = new File(input);

    }
    System.out.println("Exists");
    scanner.close();
}

The problem is that even though I input a file that exists in my computer (ex: test.txt) I enter the while loop. Is this some kind of glitch or is it something wrong with my code ?

0 Answers
Related