我在我的程序中评论了我所有的错误。我的程序的重点是通过用户输入的任何内容来移动我的输入文件。我注释掉的错误对我来说毫无意义,因为这一切都很有意义,而且计算机并没有按照我想要的方式读取它。不要与我评论其中一个标记的错误之一是“,”相混淆。其他的是“(”和“)”。这些都是错误,在我评论它们的行中的某处需要一个分号。
这是程序的样子:
import java.util.*;
import java.io.*;
class CaesarCipher
{
public static void main (String [] args) throws FileNotFoundException
{
Scanner keyboard = new Scanner(System.in);
System.out.println("What shift should I use? ");
int shift = keyboard.nextInt();
System.out.println("What is the name of the input file? ");
String name = keyboard.next();
File f = new File(name);
Scanner inFile = new Scanner(f);
System.out.println("What is the name of the output file? ");
String text = keyboard.nextLine();
PrintWriter outFile = new PrintWriter(text);
String encrypted = "";
while (inFile.hasNextLine())
{
String line = inFile.nextLine();
if ( shift == 1)
encrypted = caesarEncipher(line, shift);
else if (shift == 2)
encrypted = caesarDecipher(line, shift);// the method caesarDecipher(java.lang.String, int) is undefined for the type CaesarCipher
System.out.println(encrypted);
outFile.println(encrypted);
}
}
static String caesarEncipher(String text ,int shift) throws FileNotFoundException
{
String t = "";
int i = 0;
while (i < t.length())
{
if (shift < 0)
{
shift = (shift % 26) + 26;
}
int move = (char) ((text.charAt(0) - 'A' + shift) % 26 + 'A');
t += move;
i++;
System.out.println(t);
outFile.println(t);
return "DONE!";
}
// for each token listed, it expects the semi colon.
static String caesarDecipher(String text, int shift) throws FileNotFoundException // Syntax error on token "(", "," , ")", ; expected
{
return caesarEncipher(input, -shift);
}
}
}