Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following program uses command line arguments to create a Scanner object to

ID: 3834928 • Letter: T

Question

The following program uses command line arguments to create a Scanner object to read from a file. Fill in the blanks to complete this Java class, public class Demo{public static void main(String|| args) throws FileNotFoundException{//check and read command line argument for file name String fileName; if (args. _________ > 0) {fileName = ________;} else {fileName = _________;} _______ fileReader = openFile(fileName);} private static String promptForFileName() {System.out.println('Enter the file name: "); Scanner keyln = new Scanner(Systen.in); return keyln.next();} private static Scanner openFile(________ fileMame) throws FileNotFoundException {File file - new File(________); while (!file.exists()) {file = new File(________);} return new Scanner(____);}}

Explanation / Answer

Please find my answer:


public static void main(String[] args){
  
   String fileName;

   if(args.length > 0){
       fileName = args[0];
   }else{
       fileName = promptForFileName();
   }
}

private status Scanner openFile(String fileName)
           throws FileNotFoundException{
  
   File file = new File(fileName);

   while(!file.exists()){
       file = new File(promptForFileName());
   }

   return new Scanner(file);
}