简体   繁体   English

在令牌“,”上获得语法错误,{在此令牌之后预期

[英]Getting syntax error on token “,” , {expected after this token

I know there are already similar questions here but i tried the solutions but none worked,so i providing my code and java build path configurations settings ,so kindly help.i am using java 1.8 我知道这里已经有类似的问题,但我尝试了解决方案,但没有工作,所以我提供我的代码和java构建路径配置设置,所以亲切的帮助。我正在使用java 1.8

import java.io.File;
import java.util.ArrayList;
import java.util.List;




public class Pics {
    List<String> results = new ArrayList<String>();


    File[] files = new File("/path/to/the/directory").listFiles();
    //If this pathname does not denote a directory, then listFiles() returns null. 

    for (File file : files) {
        if (file.isFile()) {
            results.add(file.getName());
        }
    }
}

I am attaching the snapshot of the error,even though the curly braces are balanced but it still show error to insert "}" to complete class body 我正在附加错误的快照,即使花括号是平衡的但仍然显示错误插入“}”以完成类主体 错误![] [1]

构建配置

This for loop must be inside some method : 这个for循环必须在某个方法中:

for (File file : files) {
    if (file.isFile()) {
        results.add(file.getName());
    }
}

In Java, only the declarations of variables can be written directly in class but in case you want to write some logic, use the functions. 在Java中,只有变量的声明可以直接在类中编写,但是如果你想编写一些逻辑,请使用这些函数。 This is what the definition of class says: wrapper for data and functions that act on that data. 这就是类的定义所说的:用于处理该数据的数据和函数的包装器。 Additionally use constructors to provide wrapper to initializations. 另外,使用构造函数为初始化提供包装器。

import java.io.File;
import java.util.ArrayList;
import java.utilList;

public class Pics {
    List<String> results;
    File[] files = null;
    public Pics() {
        results = new ArrayList<String>();
        files = new File("/path/to/the/directory").listFiles();
    }

    //If this pathname does not denote a directory, then listFiles() returns null. 
    public void testFunction() {
        for (File file : files) {
            if (file.isFile()) {
                results.add(file.getName());
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我一直收到“令牌上的语法错误”(“,;此令牌后预期”错误? - I keep getting this “Syntax error on token ”(“, ; expected after this token” error? 令牌“ =”上的语法错误,此令牌后应为表达式 - Syntax error on token “=”, Expression expected after this token 标记“.”上的语法错误,在此标记之后超级预期 - Syntax error on token ".", super expected after this token 令牌“ [”上的语法错误,此令牌后需要表达 - syntax error on token“[”, Expression expected after this token 令牌“;”,“{”在此令牌之后的语法错误 - Syntax error on token “;”, “{” expected after this token 令牌“)”上的语法错误,此令牌后应为语句 - Syntax error on token “)”, Statement expected after this token 令牌“]”上的语法错误,此令牌后应为VariableDeclaratorld - syntax error on token “]”, VariableDeclaratorld expected after this token 令牌“ OnClickListener”上的语法错误,=预期在此令牌之后 - Syntax error on token “OnClickListener”, = expected after this token 令牌“{”上的语法错误,此令牌后预期的SwitchLabels - Syntax error on token “{”, SwitchLabels expected after this token 此标记后预期的令牌variabledeclaratorid上的语法错误 - syntax error on token variabledeclaratorid expected after this token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM