令牌上的语法错误,;预期?

时间:2012-08-07 16:23:51

标签: java

我在程序中评论了我的所有错误。我的程序的重点是移动我的输入文件 无论用户输入什么。我注释掉的错误对我来说毫无意义,因为它一切都有意义,而计算机并没有按照我想要的方式阅读它。不要混淆我评论其中一个标记的错误之一是“,”。其他的是“(”和“)”。这些都是错误,在我评论它们的行中某处有一个半冒号。

这是程序的样子:

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);
      }
     }
    }

4 个答案:

答案 0 :(得分:3)

您的caesarDecipher方法定义已嵌入方法caesarEncipher中。这不是合法的Java,你混淆了糟糕的编译器。

严格的缩进使这些事情变得非常清楚。如果您正在使用IDE或emacs,请寻找工具来重新缩进整个文件(Unix下也有命令行工具):

对于Eclipse: Ctrl + Shift + F

对于Emacs:突出显示区域(整个文件),然后: Esc Ctrl + \ Alt + 控制 + \

答案 1 :(得分:2)

你错过了方法caesar密码的结束}。使用缩进可以更快地发现这些错误。

答案 2 :(得分:0)

你在

的末尾遗漏了}
static String caesarEncipher(String text ,int shift) throws FileNotFoundException

你还有一个额外的

static String caesarDecipher(String text, int shift) throws FileNotFoundException

另请注意,当您在此^方法中返回时,您正在使用此方法中未定义的变量input。也许你会text这是你的论点

答案 3 :(得分:0)

如上所述,你错过了一个结束括号。我建议使用IDE来发现这些错误。很多人更喜欢eclipse,但我个人喜欢Intellij。 Eclipse是免费的,intellij的完整版本不是。