java出现令牌语法错误_关于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);

}

}

}

您在caesarEncipher方法中缺少右括号}

正确格式化代码可以帮助您(大多数时候)阅读计算机的工作方式。

您的caesarDecipher方法定义嵌入在方法caesarEncipher中。那不是合法的Java,而且您已经把可怜的编译器弄糊涂了。

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

对于Eclipse:Ctrl + Shift + F

对于Emacs:高亮显示区域(整个文件),然后:Esc Ctrl + 或Alt + Ctrl +

您错过了方法caesarEncipher的结尾}。使用意图更快地发现这些错误。

好的,谢谢,这是说outFile无法解决。 我在主要函数中定义了outFile,以便所有方法都可以访问它。 您知道可能是什么问题吗?

如果您在方法内的Java中声明变量(如outFile),则其范围将限于该方法。 在另一种方法内,您无法访问这些局部变量-您必须将它们作为参数传递或在另一个更广泛的范围内声明它们,例如 如果要在静态方法中使用它们,请作为静态类成员。

您如何建议我声明变量outFile。 我想最后用新转移的文字打印出来

如前所述,您缺少右括号。我建议使用IDE来发现这些错误。许多人喜欢日食,但我个人喜欢Intellij。 Eclipse是免费的,而intellij的完整版本则不是。

您在末尾缺少}

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

并且在结尾处有一个额外的

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值