-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputFilePrompt.java
More file actions
46 lines (38 loc) · 1.55 KB
/
InputFilePrompt.java
File metadata and controls
46 lines (38 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Scanner;
//This program just gather the file name
//Use methods of the String class to manipulate the user's input
//File names consist of two parts, a name and an extension
public class InputFilePrompt {
public static void main(String[] args) {
String fileName;
final String fileExtension = ".dat";
int error;
Scanner input = new Scanner(System.in);
do {
//Asks the user for the name of the input file to be read by the program
System.out.print("input file: ");
fileName = input.nextLine().trim();
String extension = fileName.substring(fileName.length()-4);
error = 0;
//The file name must be in the correct format
if (fileName.equals(fileExtension)) {
System.out.println(">>>file name missing<<<");
error++;
}
//The extension must be ".dat" (for "data")
if (!extension.equals(fileExtension)) {
System.out.println(">>>file name must end with .dat<<<");
error++;
}
//The name must not contain spaces
for (int num = 1 ; num < fileName.length() ; num++) {
if (fileName.charAt(num) == ' ') {
System.out.println(">>>file name must not contain spaces<<<");
error++;
}
}
//The user is repeatedly asked for a file name until it meets the requirement
} while (error != 0);
System.out.println("OK");
}
}