首页 > 解决方案 > 如何设置字符串变量并将其作为文件名传递给文件阅读器扫描仪?

问题描述

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

    }

}

标签: javajava.util.scannerioexception

解决方案


您的代码有问题

scanner.nextLine()你在被调用之后直接调用scanner.nextInt()

在这种情况下,如果您输入 say 5fornextInt()并按回车,那么它将返回scanner.nextInt 5 for( ) 换and行scanner.nextLine for( )scanner.nextInt . This occurs because() doesn't consume the last newline leading tonextLinehaving换行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


推荐阅读