プログラムのすべてのエラーにコメントしました。私のプログラムのポイントは、ユーザーが入力したものによって入力ファイルをシフトすることです。私がコメントアウトしたエラーは、私にとっては意味のないものです。なぜなら、それはすべて理にかなっていて、コンピューターは私が望むようにそれを読み取っていないからです。トークンの 1 つが "," であるとコメントしたエラーの 1 つと混同しないでください。それ以外は「(」と「)」です。これらはすべて、コメントした行のどこかにセミコロンが必要なエラーです。
プログラムは次のようになります。
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);
}
}
}