import java.util.Scanner;
import java.nio.file.Paths;
import java.io.*;
public class Chupapi{
public static void main(String [ ] args)throws FileNotFoundException{
new Chupapi().getLongestWords();
}
public String getLongestWords() throws FileNotFoundException{
String longWord = "";
String current;
Scanner scan = new Scanner(new File("/Users/user/Documents/PROGRAMMINGTXT/LongestWord.txt"));
while (scan.hasNext()){
current = scan.next();
if ((current.length() > longWord.length()) && (!current.matches(".*\\d.*"))) {
longWord = current;
}
}
System.out.println("Longest word: "+longWord);
longWord.replaceAll("[^a-zA-Z ]", "").split("\\s+");
return longWord;
}
}
I want to add a line where the User will need to enter the specific file name like "Enter the file name: LongestWord.txt" then outputs the LongestWord but if the user didn't enter the specific file name it will be like "Filename incorrect!" what loop should I use?