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

旧城冷巷雨未停 提交于 2019-12-05 05:14:20

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:

  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 system should be System.

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

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 link for more details.

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

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);

}

}

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"); }'

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");
    }}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!