catch32
catch32

Reputation: 18582

How do check if user-input path is correct?

I need to be prove about user-input path.
Coz when user give not folder path but file path. Program "fall down".
I understand that this is a bug but how to be ensure about user path correct.

Code:

 class PathAndWord {
    final String path;
    final String whatFind;

    PathAndWord(String path, String whatFind) {
        this.path = path;
        this.whatFind = whatFind;
    }

    boolean isProperlyInitialized() {
        return path != null && whatFind != null;
    }
}

public void askUserPathAndWord() {
    try {
        tryToAskUserPathAndWord();
    } catch (IOException | RuntimeException e) {
        System.out.println("Wrong input!");
        e.printStackTrace();
    } catch (InterruptedException e) {
        System.out.println("Interrupted.");
        e.printStackTrace();
    }
}

private void tryToAskUserPathAndWord() throws IOException, InterruptedException {
    PathAndWord pathAndWord = readPathAndWord();

    if (pathAndWord.isProperlyInitialized()) {
        performScan(pathAndWord, "GameOver.tmp");
        System.out.println("Thank you!");
    } else {
        System.out.println("You did not enter anything");
    }
}

private PathAndWord readPathAndWord() throws IOException {
    System.out.println("Please, enter a Path and Word (which you want to find):");

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

    String path = readPath(bufferedReader);
    String whatFind = readWord(bufferedReader);
    return new PathAndWord(path, whatFind);
}

private String readPath(BufferedReader bufferedReader) throws IOException {
    System.out.println("Please enter a Path:");
    return bufferedReader.readLine();
}

private String readWord(BufferedReader bufferedReader) throws IOException {
    System.out.println("Please enter a Word:");
    return bufferedReader.readLine();
}

private void performScan(PathAndWord pathAndWord, String endOfWorkFileName) throws InterruptedException {
    BlockingQueue<File> queue = new LinkedBlockingQueue<File>();

    File endOfWorkFile = new File(endOfWorkFileName);
    CountDownLatch latch = new CountDownLatch(2);

    FolderScan folderScan = new FolderScan(pathAndWord.path, queue, latch,
            endOfWorkFile);
    FileScan fileScan = new FileScan(pathAndWord.whatFind, queue, latch,
            endOfWorkFile);

    Executor executor = Executors.newCachedThreadPool();
    executor.execute(folderScan);
    executor.execute(fileScan);

    latch.await();
}

Qustions:

Upvotes: 0

Views: 3221

Answers (1)

pkopac
pkopac

Reputation: 1035

private String readPath(BufferedReader bufferedReader) throws IOException {
    boolean ok = false;
    do {
        System.out.println("Please enter a Path:");
        File f = new File(bufferedReader.readLine());
        if(f.exists() && f.isDirectory())
            ok = true;
        else
            System.err.println("Doesn't exist or is not a folder.");
    } while(!ok);
    return f.getAbsolutePath();
}

EDIT: This method does a task "read a path from user, which exists and is a directory". If the user types invalid path (non-existent or a file) the method recognizes this, warns the user and asks them again... and again and again - until they answer correctly.

It's a good custom to check data locally, if you can. When calling the method later you can be sure it returns, what you expect.

Upvotes: 1

Related Questions