java 令牌上的语法错误,;预期的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11850178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error on tokens, ; expected?
提问by Alex Mallo
I commented all my errors in my program. The point of my program is to shift my input file by whatever the user inputs. My errors that I commented out are kind of pointless to me because it all make sense and the computer is not reading it the way I want it to. Not to be confused with one of the errors that I commented one of the tokens is ",". The others are "(", and ")". These all are errors that expect a semi colon somewhere in the line where I commented them.
我在我的程序中评论了我的所有错误。我的程序的重点是通过用户输入的任何内容来移动我的输入文件。我注释掉的错误对我来说毫无意义,因为这一切都是有道理的,而且计算机并没有按照我想要的方式读取它。不要与我评论的标记之一是“,”的错误之一混淆。其他的是“(”和“)”。这些都是错误,需要在我评论它们的行中的某处使用分号。
Here is what the program looks like:
这是程序的样子:
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);
}
}
}
回答by Richard Sitze
Your caesarDecipher
method definition is embedded in the method caesarEncipher
. That's not legal Java, and you've confused the poor compiler.
您的caesarDecipher
方法定义嵌入在方法中caesarEncipher
。那不是合法的 Java,而且您已经混淆了糟糕的编译器。
Strict indenting makes these kinds of things very clear. If you're using an IDE, or emacs, look for tooling to re-indent your entire file (there are also command line tools under Unix):
严格的缩进使这些事情变得非常清楚。如果您使用的是 IDE 或 emacs,请寻找重新缩进整个文件的工具(Unix 下也有命令行工具):
For Eclipse: Ctrl+Shift+F
对于Eclipse:Ctrl+ Shift+F
For Emacs: Highlight region (entire file), then: EscCtrl+\OR Alt+Ctrl+\
对于 Emacs:突出显示区域(整个文件),然后:EscCtrl+ \OR Alt+ Ctrl+\
回答by Pyranja
you missed the closing } on your method caesarEncipher. Use intendation to spot these errors faster.
你错过了你的方法 caesarEncipher 的结束 }。使用意图更快地发现这些错误。
回答by La bla bla
You're missing }
in the end of your
你在你}
的尽头失踪
static String caesarEncipher(String text ,int shift) throws FileNotFoundException
and you have an extra one in the end of
并且你在最后有一个额外的
static String caesarDecipher(String text, int shift) throws FileNotFoundException
also note, when you are returning in this ^ method, you're using the variable input
which is undefined in this method. maybe you ment text
which is your argument
另请注意,当您在此 ^ 方法中返回时,您正在使用input
此方法中未定义的变量。也许你知道text
这是你的论点
回答by nook
As said, you are missing a closing brace. I would recommend using a IDEto spot these mistakes. Many people prefer eclipsebut I personally like Intellij. Eclipse is free, and the full version of intellij is not.
如上所述,您缺少一个右括号。我建议使用IDE来发现这些错误。许多人更喜欢eclipse,但我个人喜欢Intellij。Eclipse 是免费的,而完整版的 intellij 则不是。