令牌“(”上的Java错误语法错误;应为ub Java函数
嗨,我的Java代码中出现了这个错误 令牌上出现语法错误;应为 我正在尝试创建一个函数,可能我的语法不正确。 这是我的代码:我指向错误所在的位置令牌“(”上的Java错误语法错误;应为ub Java函数,java,function,syntax,Java,Function,Syntax,嗨,我的Java代码中出现了这个错误 令牌上出现语法错误;应为 我正在尝试创建一个函数,可能我的语法不正确。 这是我的代码:我指向错误所在的位置 import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; p
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
String ruta = "C:\\Users\\HernanEi\\Desktop\\contadoresInternet.txt";
File archivo = new File(ruta);
String linea = null;
try {
FileReader lector = new FileReader(archivo);
BufferedReader buff = new BufferedReader(lector);
while( ( linea = buff.readLine() ) != null ) {
System.out.println(linea);
}
buff.close();
lector.close();
} catch(FileNotFoundException ex) {
} catch(IOException ex) {
}
final int countWord(String codigo, File archivo)<-------Error Here
{
int count = 0;
Scanner scanner = new Scanner("C:\\Users\\HernanEi\\Desktop\\contadoresInternet.txt");
while (scanner.hasNextLine()) {
String nextToken = scanner.next();
if (nextToken.equalsIgnoreCase(codigo))
count++;
}
return count;
}
}
}
很抱歉,如果这是一个非常简单的问题,这都在我的主类中。将}从代码底部移动到主方法的末尾
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
String ruta = "C:\\Users\\HernanEi\\Desktop\\contadoresInternet.txt";
File archivo = new File(ruta);
String linea = null;
try {
FileReader lector = new FileReader(archivo);
BufferedReader buff = new BufferedReader(lector);
while( ( linea = buff.readLine() ) != null ) {
System.out.println(linea);
}
buff.close();
lector.close();
} catch(FileNotFoundException ex) {
} catch(IOException ex) {
}
}
final int countWord(String codigo, File archivo){
int count = 0;
Scanner scanner = new Scanner("C:\\Users\\HernanEi\\Desktop\\contadoresInternet.txt");
while (scanner.hasNextLine()) {
String nextToken = scanner.next();
if (nextToken.equalsIgnoreCase(codigo))
count++;
}
return count;
}
}
我重新格式化了您的代码,因此错误变得更加明显
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class HelloWorld {
public static void main(String... args) {
String ruta = "C:\\Users\\HernanEi\\Desktop\\contadoresInternet.txt";
File archivo = new File(ruta);
String linea = null;
try {
FileReader lector = new FileReader(archivo);
BufferedReader buff = new BufferedReader(lector);
while ((linea = buff.readLine()) != null) {
System.out.println(linea);
}
buff.close();
lector.close();
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
} // Moved this parenthesis up
final int countWord(String codigo, File archivo) { // <-------Error Here
int count = 0;
Scanner scanner
= new Scanner("C:\\Users\\HernanEi\\Desktop\\contadoresInternet.txt");
while (scanner.hasNextLine()) {
String nextToken = scanner.next();
if (nextToken.equalsIgnoreCase(codigo)) {
count++;
}
}
return (count);
}
}
关于代码的一些备注:
你应该看一看。
您肯定应该使用英文变量/属性名称编写代码
即使您可以忽略一些括号,例如只有一行代码的ifs,您也应该编写它们以保持清晰
主方法上缺少一个右大括号,就在给出错误消息的那一行之前。不幸的是,有时语法错误在发生时并不明显,最终会导致以后出现错误,因此编译器的错误消息可能会产生误导
使用一个理解该语言的好编辑器是有帮助的。您可能已经在这样做了。如果是这样,那么编辑器将countWord定义的第一行放置在与主方法体相同的级别,这表明您没有正确关闭后者。在尝试定义之前,您需要关闭主方法另一个。也就是说,你在错误行前缺少了一个。}。谢谢,你是对的。非常感谢图灵85,我现在看到了,我会确保听从你的建议。谢谢,你是对的。非常感谢斯特凡