gpt4 book ai didi

java - 使用文件的凯撒密码

转载 作者:行者123 更新时间:2023-12-01 09:14:27 24 4
gpt4 key购买 nike

我正在尝试编写一个使用凯撒密码对纯文本进行编码的java程序。我有一个包含纯文本的文件和另一个应放置编码文本的文件。我不知道问题是什么,但出现错误:线程“main”java.util.NoSuchElementException 中出现异常。请帮助我!

  import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class zevendesim {

public static void main(String[] args)
throws FileNotFoundException {
Scanner input=new Scanner(new File("C:\\erida\\t.txt"));
PrintStream output=new PrintStream(new File("C:\\erida\\c.txt"));
String text=input.nextLine();
zevendesim(text, output);
}
public static void zevendesim(String text, PrintStream output)
{

int i=0;
String s;
Scanner data = new Scanner(text);
while (data.hasNext()) {
s=data.next();
if (s.charAt(i) >= 97 && s.charAt(i)<= 120)
{
int x = s.charAt(i) - 97;
x = (x + 2) % 26;
if (x < 0)
x += 26;
// = (char) (x + 32);
}
output.print(" " + data.next());
}

}
}

请帮我找到解决办法!

错误堆栈跟踪是:

Exception in thread "main" java.util.NoSuchElementException at
java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at Zevendesimi.zevendesim.zevendesim(zevendesim.java:33) at Zevendesimi.zevendesim.main(zevendesim.java:15)

最佳答案

错误发生在 output.print(""+ data.next()); 中,因为最后一个标记已在几行之前使用 s=data.next( );

您可能想将 x 写入输出?

关于java - 使用文件的凯撒密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40674421/

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