gpt4 book ai didi

java - 如何设置字符串变量并将其作为文件名传递到文件读取器扫描仪中?

转载 作者:行者123 更新时间:2023-12-02 10:56:46 24 4
gpt4 key购买 nike

我正在编写一个密码程序,它要求用户提供文件名,将该名称存储为变量,然后根据字符串变量读取文件。它可以编译,但当我尝试运行它时,我不断收到 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()叫做 。

在这种情况下,如果您输入 5对于 nextInt()然后按回车键,就会返回 5 for扫描仪.nextInt() and新线for扫描仪.nextLine() . This occurs because扫描仪.nextInt() doesn't consume the last newline leading to下一行 having换行 . 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() 之前,添加另一个scanner.nextLine()在它上面只是为了消耗最后一个新行。

就文件名而言,使用绝对路径运行程序应该可以正常运行:如 D:\Work\file.txt

关于java - 如何设置字符串变量并将其作为文件名传递到文件读取器扫描仪中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51666907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com