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

Java 如何设置字符串变量并将其作为文件名传递到文件读取器扫描仪?,java,java.util.scanner,ioexception,Java,Java.util.scanner,Ioexception,我正在编写一个密码程序,它要求用户输入文件名,将文件名存储为变量,然后根据字符串变量读取文件。它可以编译,但当我尝试运行它时,我总是会收到一个IOException java.io.FileNotFoundException: (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.j

我正在编写一个密码程序,它要求用户输入文件名,将文件名存储为变量,然后根据字符串变量读取文件。它可以编译,但当我尝试运行它时,我总是会收到一个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)
java.io.FileNotFoundException:(没有这样的文件或目录)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(FileInputStream.java:146)
位于java.io.FileInputStream。(FileInputStream.java:101)
位于java.io.FileReader。(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;

    }

}
import java.util.Scanner;
导入java.io.*;
公共类密码{
公共静态void main(字符串[]args)引发FileNotFoundException{
字符串infle=“”;
扫描仪sc=新的扫描仪(System.in);
System.out.println(“欢迎使用Caeser密码”);
System.out.println(“输入1加密,或输入2解密(-1退出):”;
int cipher=sc.nextInt();
System.out.println(“我应该使用什么非负移位?”);
int shift=sc.nextInt();
System.out.println(“输入文件名是什么?”);
infle=sc.nextLine();
试一试{
扫描仪输入=新扫描仪(新文件读取器(内嵌));
String line=input.nextLine();
/*System.out.println(“输出文件名是什么?”);
字符串outFile=sc.nextLine()*/
扫描仪输入=新扫描仪(新文件读取器(内嵌));
输入。使用分隔符(“[”!?0-9+”);
String line=input.nextLine();
while(input.hasNextLine()){
line=input.nextLine();
如果(密码==1){
系统输出打印LN(caeserEncipher(线路、班次));
}else if(密码==2){
System.out.println(caeserdecyper(line,shift));
}否则{
System.out.println(“输入1进行加密,或输入2进行解密(-1退出)”;
返回;
}
}
}
catch(filenotfounde异常){
System.out.println(“打开或读取文件时出现问题…”);
System.out.println(“可能是拼错了!”);
e、 printStackTrace();
}
}
公共静态字符串caeserEncipher(字符串输入,整数移位){
int arr[]=new int[input.length()];
StringBuilder=新的StringBuilder();
int length=input.length();
字符串输出=”;
for(int i=0;i
代码问题

在调用
scanner.nextLine()
之后,您将直接调用
scanner.nextLine()

在这种情况下,如果为
nextLine()
输入say
5
,然后按enter键,那么它将为
scanner.nextInt()
返回
5,并为
scanner.nextLine()
返回
新行
。发生这种情况的原因是
scanner.nextLine()
不会使用导致
nextLine
的最后一个换行符,因此,在您的情况下,用户将没有机会输入导致错误的文件名,并且似乎跳过了
scanner.nextLine()`调用

因此,在调用
scanner.nextLine()
之前,请在其上方添加另一个
scanner.nextLine()
以使用最后一行新行


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

您的代码有很多错误

  • 输入的多次声明
  • 错误的正则表达式。
    “[”!?0-9+”
    应为
    “\\[”!?0-9+”
  • scanner.nextLine()之后取消管理换行符(回车)
  • 请尝试此修改后的代码:-

    import java.util.Scanner;
    import java.io.*;
    
    public class Cipher {
    
        public static void main(String[] args) throws FileNotFoundException {
    
            String inFile = "";
    
            // avoiding multtiple delcaration.
            Scanner input = null;
            String line = "";
    
            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();
    
            sc.nextLine(); // clearing '\n' (newline)
            System.out.println("What is the input file name?");
            inFile = sc.nextLine();
            try {
    
                input = new Scanner(new FileReader(inFile));
                line = input.nextLine();
    
                /*
                 * System.out.println("What is the output file name?"); String outFile =
                 * sc.nextLine();
                 */
    
                input = new Scanner(new FileReader(inFile));
                input.useDelimiter("\\['.!?0-9+");            // changed reg-expression
    
                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;
    
        }
    
    }
    
    import java.util.Scanner;
    导入java.io.*;
    公共类密码{
    公共静态void main(字符串[]args)引发FileNotFoundException{
    字符串infle=“”;
    //避免多次去角质化。
    扫描仪输入=空;
    字符串行=”;
    扫描仪sc=新的扫描仪(System.in);
    System.out.println(“欢迎使用Caeser密码”);
    System.out.println(“输入1加密,或输入2解密(-1退出):”;
    int cipher=sc.nextInt();
    System.out.println(“我应该使用什么非负移位?”);
    int shift=sc.nextInt();
    sc.nextLine();//清除'\n'(换行符)
    System.out.println(“输入文件名是什么?”);
    infle=sc.nextLine();
    试一试{
    输入=新扫描仪(新文件读取器(内嵌));
    line=input.nextLine();
    /*
    *System.out.println(“输出文件名是什么?”);字符串输出文件=
    *sc.nextLine();
    */
    输入=新扫描仪(新文件读取器(内嵌));
    input.useDelimiter(“\\[”.!?0-9+”;//更改了reg表达式
    line=input.nextLine();
    while(input.hasNextLine()){
    line=input.nextLine();
    如果(密码==1){
    系统输出