java - 如何设置字符串变量并将其作为文件名传递给文件阅读器扫描仪?
问题描述
我正在编写一个密码程序,它要求用户输入文件名,将名称存储为变量,然后应该根据字符串变量读取文件。它可以编译,但是当我尝试运行它时,我不断收到 IOException。
java.io.FileNotFoundException: (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at Cipher.main(Cipher.java:22)
我不明白发生了什么。它在用户更改键入文件名之前给出错误。这是我的代码:
import java.util.Scanner;
import java.io.*;
public class Cipher {
public static void main (String [] args) throws FileNotFoundException {
String inFile = "";
Scanner sc = new Scanner (System.in);
System.out.println("Welcome to Caeser Cipher");
System.out.println("Enter 1 to encipher, or 2 to decipher (-1 to exit): ");
int cipher = sc.nextInt();
System.out.println("What non-negative shift should I use?");
int shift = sc.nextInt();
System.out.println("What is the input file name?");
inFile = sc.nextLine();
try {
Scanner input = new Scanner (new FileReader (inFile) ) ;
String line = input.nextLine();
/* System.out.println("What is the output file name?");
String outFile = sc.nextLine();*/
Scanner input = new Scanner (new FileReader (inFile) ) ;
input.useDelimiter("['.!?0-9+");
String line = input.nextLine();
while (input.hasNextLine()) {
line = input.nextLine();
if (cipher == 1) {
System.out.println(caeserEncipher(line, shift));
} else if (cipher == 2) {
System.out.println(caeserDecipher(line, shift));
} else {
System.out.println ("Enter 1 to encipher, or 2 to decipher (-1 to exit)");
return;
}
}
}
catch (FileNotFoundException e) {
System.out.println ("Trouble opening or reading the file...");
System.out.println ("Perhaps it was misspelled!");
e.printStackTrace();
}
}
public static String caeserEncipher(String input, int shift) {
int arr[] = new int[input.length()];
StringBuilder builder = new StringBuilder();
int length = input.length();
String output = "";
for (int i = 0; i < length; i++) {
arr[i] = (int) input.charAt(i);
arr[i] += shift;
}
for (int i = 0; i < length; i++) {
builder.append((char)arr[i]);
}
output = builder.toString();
return output;
}
public static String caeserDecipher(String input, int shift) {
int arr[] = new int[input.length()];
StringBuilder builder = new StringBuilder();
int length = input.length();
String output = "";
for (int i = 0; i < length; i++) {
arr[i] = (int) input.charAt(i);
arr[i] -= shift;
}
for (int i = 0; i < length; i++) {
builder.append((char)arr[i]);
}
output = builder.toString();
return output;
}
}
解决方案
您的代码有问题
scanner.nextLine()
你在被调用之后直接调用scanner.nextInt()
。
在这种情况下,如果您输入 say 5
fornextInt()
并按回车,那么它将返回scanner.nextInt 5 for
( ) 换and
行scanner.nextLine for
( )scanner.nextInt . This occurs because
() doesn't consume the last newline leading to
nextLinehaving
换行scanner.nextLine . So, in your case, user will not get a chance to input the file-name leading to error and it would seem that
()` 调用被跳过。
所以在调用之前scanner.nextLine()
,在它上面添加另一个scanner.nextLine()
只是为了消耗最后一个新行。
就文件名而言,使用绝对路径运行程序,它应该可以正常运行:比如D:\Work\file.txt
推荐阅读
- mongodb - 在 mac os 10.15 for php7 上安装 mongodb
- android - 如何从 viewpager 的片段中的 sqlite 检索一行?
- dotnetnuke - 没有堆栈跟踪的 DNN PageLoadException
- python - Python:你能动态获取函数要返回的变量数量吗?
- java - writeBytes 放入 CR
- java - 有没有办法在没有 RBAC 的情况下获取集群中的所有 pod?
- javascript - Vue Native 的全局样式
- json - Angular 8 - 比较对象内的数组(JSON)的更好方法是什么?
- tsql - 这是处理存储过程中可选参数的好方法吗?
- c - 使用修饰符时的 VT100 数字小键盘转义码(ctrl/shift/alt)