Java 标记“;”上的语法错误,{ 预期在随机字符串创建者中的此标记之后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18836404/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:30:46  来源:igfitidea点击:

Syntax error on token ";", { expected after this token in Random string creator

javasyntaxsyntax-errorbrackets

提问by

I am writing code to generate a random 3 letter strings using the letters a, b, and c. I am getting the error message "Syntax error on token ";", { expected after this token" after the line where i create the random variable (Random rand = new Random();). I do not know why I am getting this error when it looks fine to me.

我正在编写代码以使用字母 a、b 和 c 生成随机的 3 个字母字符串。我在创建随机变量 (Random rand = new Random();) 的行之后收到错误消息“令牌语法错误”;“,{ 预期在此令牌之后”。我不知道为什么当我看起来不错时会收到此错误。

I am also getting the error message: Syntax error, insert "}" to complete ClassBody, after the last bracket in the program. I am almost postive all my closing brackets match up so I do not know where this error is coming from. PLEASE HELP!!

我还收到错误消息:语法错误,在程序的最后一个括号之后插入“}”以完成 ClassBody。我几乎所有的右括号都匹配,所以我不知道这个错误是从哪里来的。请帮忙!!

 import java.util.*;


 public class Orders {

String alphabet = "abc";
ArrayList<String> list = new ArrayList<String>();
int n = alphabet.length();

Random rand = new Random();
for (int i = 0; i < 10000; i++){
    char a = alphabet.charAt(rand.nextInt(n));
    char b = alphabet.charAt(rand.nextInt(n));
    char c = alphabet.charAt(rand.nextInt(n));

    String s = Character.toString(a) + Character.toString(b) + Character.toString(c); 

    if(list.indexOf(s) == -1){
        list.add(s);
    }
}
 system.out.println(arrayList);
}

回答by Juned Ahsan

In Java, you cannot directly write the executable statements in class. You need to move your code in a method. Only variables declaration is allowed outside the method/blocks. Just for the sake of testing, ,move everthing to main method. Try this:

在 Java 中,您不能直接在类中编写可执行语句。您需要在方法中移动代码。在方法/块之外只允许变量声明。只是为了测试,将所有内容移至 main 方法。尝试这个:

  public class Orders {

        public static void main(String argsp[]) {
            String alphabet = "abc";
            ArrayList<String> list = new ArrayList<String>();
            int n = alphabet.length();

            Random rand = new Random();
            for (int i = 0; i < 10000; i++){
               char a = alphabet.charAt(rand.nextInt(n));
               char b = alphabet.charAt(rand.nextInt(n));
               char c = alphabet.charAt(rand.nextInt(n));

               String s = Character.toString(a) + Character.toString(b) + Character.toString(c); 

               if(list.indexOf(s) == -1){
                   list.add(s);
               }
            }
            System.out.println(list);
        }

}

Note: system.out.println(arrayList);will throw an error because there is no varaible called arrayList, i think it should be replaced with variable list. Also systemshould be System.

注意system.out.println(arrayList);会抛出错误,因为没有调用arrayList变量,我认为应该用变量替换它list。也system应该是System

回答by Prabhaker A

In java you can't simply code loops and other actions as part of the class definition, but rather as method/constructor/block definitions inside the class

在 Java 中,您不能简单地将循环和其他操作编码为类定义的一部分,而是作为类中的方法/构造函数/块定义

for (int i = 0; i < 10000; i++){
    char a = alphabet.charAt(rand.nextInt(n));
    char b = alphabet.charAt(rand.nextInt(n));
    char c = alphabet.charAt(rand.nextInt(n));

    String s = Character.toString(a) + Character.toString(b) + Character.toString(c); 

    if(list.indexOf(s) == -1){
        list.add(s);
    }
}
 system.out.println(arrayList);  

So this code should be in method/constructor/block.
For example in method

所以这段代码应该在方法/构造函数/块中。
例如在方法中

public void printList(){
 for (int i = 0; i < 10000; i++){
        char a = alphabet.charAt(rand.nextInt(n));
        char b = alphabet.charAt(rand.nextInt(n));
        char c = alphabet.charAt(rand.nextInt(n));

        String s = Character.toString(a) + Character.toString(b) + Character.toString(c); 

        if(list.indexOf(s) == -1){
            list.add(s);
        }
    }
     system.out.println(arrayList);  
}

Please refer this linkfor more details.

请参阅此链接了解更多详情。

回答by Sapna

I was getting a error "invalid token 'manage()..." when i coded as below .That is when i wrote my code directly to a class.

当我如下编码时,我收到一个错误“无效令牌'管理()......”。那是我将代码直接写入类的时候。

//before

//前

public class test{ WebDriver driver=new FirefoxDriver(); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); driver.get("http://http://www.extjs-tutorial.com/live-examples/extjs4/CRUD-in-Form-model/default.htm"); }'

public class test{ WebDriver driver=new FirefoxDriver(); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); driver.get("http://http://www.extjs-tutorial.com/live-examples/extjs4/CRUD-in-Form-model/default.htm"); }'

After i moved my code to a method , it worked

在我将代码移到方法后,它起作用了

//after

//后

 public class test{
    public void setup(){
    WebDriver driver=new FirefoxDriver();
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.get("http://http://www.extjs-tutorial.com/live-examples/extjs4/CRUD-in-Form-model/default.htm");
    }}

回答by Klayd Pro

The reason you get a syntax error in that code is that you have an extra bracket. to the code above the error!

在该代码中出现语法错误的原因是您有一个额外的括号。到错误上面的代码!

回答by Arslan Ahmad

You just need to write your code inside the main function.
You just miss the main function.

你只需要在main 函数中编写你的代码。
你只是错过了主要功能

Your code

你的代码

import java.util.*;
 public class Orders {//Include main function

 String alphabet = "abc";
ArrayList<String> list = new ArrayList<String>();
int n = alphabet.length();

Random rand = new Random();
for (int i = 0; i < 10000; i++){
    char a = alphabet.charAt(rand.nextInt(n));
    char b = alphabet.charAt(rand.nextInt(n));
    char c = alphabet.charAt(rand.nextInt(n));

    String s = Character.toString(a) + Character.toString(b) + Character.toString(c); 

    if(list.indexOf(s) == -1){
        list.add(s);
    }
}
 system.out.println(arrayList);
}

Correct code

正确的代码

package testprob;
import java.util.*;
public class testprob {
public static void main(String arrg[]) {//you need to add main function 
     String alphabet = "abc";
     ArrayList<String> list = new ArrayList<String>();
     int n = alphabet.length();

     Random rand = new Random();
     for (int i = 0; i < 10000; i++){
         char a = alphabet.charAt(rand.nextInt(n));
         char b = alphabet.charAt(rand.nextInt(n));
         char c = alphabet.charAt(rand.nextInt(n));

         String s = Character.toString(a) + Character.toString(b) + Character.toString(c); 

         if(list.indexOf(s) == -1){
             list.add(s);
         }
     }
      System.out.println(list);

}

}