gpt4 book ai didi

java - 为什么我不断收到 "must be caught or declared to be thrown"错误?

转载 作者:行者123 更新时间:2023-12-01 19:06:50 25 4
gpt4 key购买 nike

这是我的代码

import java.io.*;
import java.util.*;
import java.util.Scanner;
import java.io.PrintWriter;

public class EncryptionDecryption {

public static void main(String[] args) throws java.io.IOException{

int z = getRandom();
boolean luck = true;

while(luck == true){
String codeString = getString();
System.out.println(codeString);

char[] enCharArray = encrypt(codeString, z);
String encryptedString = new String(enCharArray);
System.out.println(encryptedString);

char[] deCharArray = decrypt(encryptedString, z);
String decryptedString = new String(deCharArray);
System.out.println(decryptedString);
putString(encryptedString);

if(codeString.length() == 0)
luck = false;
}
}

static String getString(){
Scanner input = new Scanner(new File(" "));
String codeString = input.next();
return codeString;
}

static void putString (String finalString){
PrintWriter work = new PrintWriter("EncryptedDocument.txt");
work.print(finalString + " ");
work.close();
}

static char[] encrypt(String encryptString, int z){
char[] codeChar = encryptString.toCharArray();
char[] enCharArray;
enCharArray = new char[codeChar.length];
for(int i = 0; i < codeChar.length; i++){
int x = codeChar[i];
int enInt = encryptChar(x, z);
char enChar = (char)enInt;
enCharArray[i] = enChar;
if(x == 32){
enInt = 32;
enChar = (char)enInt;
enCharArray[i] = enChar;
}
}
return enCharArray;
}

static char[] decrypt(String decryptString, int z){
char[] deCodeChar = decryptString.toCharArray();
char[] deCharArray;
deCharArray = new char[deCodeChar.length];
for(int i = 0; i < deCodeChar.length; i++){
int x = deCodeChar[i];
int deInt = decryptChar(x, z);
char deChar = (char)deInt;
deCharArray[i] = deChar;
if(x == 32){
deInt = 32;
deChar = (char)deInt;
deCharArray[i] = deChar;
}
}
return deCharArray;
}

static int encryptChar(int x, int z){
int y = 'A';
int enInt = (x - y + z) % 26 + y;
return enInt;
}

static int decryptChar(int x, int z){
int y = 'A';
int deInt = (x - y + 104 - z) % 26 + y;
return deInt;
}

static int getRandom(){
int encryptMethod = 0;
while(encryptMethod == 0){
Random encrypt = new Random();
encryptMethod = encrypt.nextInt(96);
}
return encryptMethod;
}

}

当我尝试编译时,我不断收到这些错误:

EncryptionDecryption.java:32: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Scanner input = new Scanner(new File(" "));
^
EncryptionDecryption.java:38: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
PrintWriter work = new PrintWriter("EncryptedDocument.txt");
^
2 errors

最佳答案

因为您调用的方法声明抛出 FileNotFoundException,并且您没有捕获该异常,也没有声明封闭方法抛出该异常。这在 Java 中是不允许的。所有检查的异常都必须被捕获,或者在方法的 throws 子句中声明:

static String getString() throws FileNotFoundException {

如果您可以处理异常并做一些有意义的事情使您的程序继续按预期工作,那么捕获异常。如果您无法在此方法中处理它,则让方法的调用者为您处理它,并通过在 throws 子句中声明它来让它传播。

关于java - 为什么我不断收到 "must be caught or declared to be thrown"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9734051/

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