0
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?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

0

You could use an if() else loop.

A possible implementation in your main function would then look like

  public static void main(String[] args) throws FileNotFoundException {

    final String correctFileName = "LongestWord.txt";
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the file name: " + correctFileName);
    String s = scan.nextLine();
    if (s.equals(correctFileName)) {
      new Chupapi().getLongestWords();
    } else {
      System.out.println("Filename incorrect!");
    }
  }

Note: To make the file name dynamic you could set it as a variable outside the main function and use the name in the getLongestWords() function.

final String correctFileName = "LongestWord.txt";

main (){}
getLongestWords (){
...
Scanner scan = new Scanner(new File("/Users/user/Documents/PROGRAMMINGTXT/" + correctFileName));
...
}

Also, this assumes the file is always is the path /Users/user/Documents/PROGRAMMINGTXT/

0

I used the if() else loop and it somehow got what I wanted to do with it. Thank you guys @OneCricketeer and @Tcheutchoua Steve

import java.util.Scanner;
import java.io.*;

public class Chupapi{
public static void main(String [ ] args)throws FileNotFoundException{
    new Chupapi().getLongestWords();
}

public String getLongestWords() throws FileNotFoundException{

    Scanner scanner = new Scanner (System.in);
    System.out.print("Enter the specific filename: ");
    String filename = scanner.next();
        String longWord = "";
        String current;
        Scanner scan = new Scanner(new File("/Users/glenn/Documents/PROGRAMMINGTXT/LongestWord.txt"));
        while (scan.hasNext()) {
            current = scan.next();
            if ((current.length() > longWord.length()) && (!current.matches(".*\\d.*"))) {
                longWord = current;
            }
        }
        if (filename.equals("LongestWord.txt")) {
            System.out.println("Pinakamahabang salita: " + longWord);
            longWord.replaceAll("[^a-zA-Z ]", "").split("\\s+");
            return longWord;
        }
     else {
            System.out.println("Incorrect filename!");
            return filename;
     }
}
}