Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do check if user-input path is correct?
    text
    copied!<p>I need to be prove about user-input path.<br> Coz when user give not folder path but file path. Program "fall down".<br> I understand that this is a bug but how to be ensure about user path correct.</p> <p><strong>Code:</strong> </p> <pre><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 &amp;&amp; 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&lt;File&gt; queue = new LinkedBlockingQueue&lt;File&gt;(); 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(); } </code></pre> <p><strong>Qustions:</strong> </p> <ul> <li>How do check if user input <code>path</code> is correct? </li> <li>If path isn't correct to show message that <code>path is wrong! Try again</code>. </li> <li>Able to check word - <code>whatFind</code> whether it's correct, too. </li> <li>Does need before point do?</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload