Java program that prompts the user for a drive letter, a path, a file name, and a extension then print the complete file name
Posted by Samath
Last Updated: February 09, 2021
  668

File names and extensions. Write a Java program that prompts the user for the drive letter ( C ), the path ( \Windows\System ), the file name ( Readme ), and the extension ( txt ). Then print the complete file name  C:\Windows\System\Readme.txt . (If you use UNIX or a Macintosh, skip the drive name and use  / instead of \ to separate directories.)

Sample Output:
Please enter the drive letter: C                                                                                                                                                           
Please enter the path: programs                                                                                                                                                            
Please enter the filename: game                                                                                                                                                            
Please enter the extension: exe                                                                                                                                                            
C:\programs\game.exe  


Code:

import java.util.Scanner;

public class Main
{
         public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.print("Please enter the drive letter: ");
            String driveLetter = in.next();
            
            System.out.print("Please enter the path: ");
            String path = in.next();
            
            System.out.print("Please enter the filename: ");
            String filename = in.next();
            
            System.out.print("Please enter the extension: ");
            String extension = in.next();
            
            in.close();
            String completeFilename = driveLetter +":"+ "\\"  + path + "\\" + filename + "." + extension;
            System.out.println(completeFilename);
    }
}