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

syntax error on token identifier expected after this token
syntax error on token '', ; expected in jsp
syntax error on token misplaced construct
syntax error on token else
syntax error on token talend
syntax error on token void'', @ expected
syntax error on token =, name expected after this token
syntax error on token ( expression expected after this token

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.

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!!

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

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.

Syntax error on token ";", { expected after this token, The reason you get a syntax error in that code is that you have an extra bracket. Try the following code: 1. package org.temp2.cod1; 2. import  I am Venkatesh, have experience 9+ years on IT industry, good exposure on various technologies and mainly my javaprogramto.com blog provides articles on core java in-depth, Day to day common errors, exceptions, troubleshooting, programs, Interview Questions and Answers, User requests.

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.

[SOLVED] Syntax error on token (Beginning Java forum at Coderanch), Syntax error on token "foo", Identifier expected after this token. Which occurs on line 4 in the Main. I've done some Google searches and most of  What causes Syntax Error near unexpected token ` (‘? Solution 1: Checking Syntax and Format of commands. Solution 2: Troubleshooting your shell script. Solution 3: Using ‘dos2unix.exe’ command. In Windows/DOS text files,

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

}

}

Syntax error on token, Hoping someone could help me resolve this error I'm getting in Eclipse. Any help would be appreciated! On the 1st line i'm getting: - Syntax  That's just a statement - but it's not in a constructor, method or other initializer. It's not clear why you don't just assign a value at the point of the declaration: private MediaPlayer mPlayer = new MediaPlayer(); I'd also recommend removing the redundant semi-colon at the end of the enum declaration. Share a link to this answer.

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

Solved, Hello everyone! So i was trying to do an report plugin but it gives me the error "​Syntax error on token "}", { expected after this token " on line 40,. Error Syntax error on token "default", delete this token while working with Interfaces and Java 8 Error Type Compile Time Possible

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

syntax error on token . @ expected after this token, "Syntax error on token "else", delete this token" ERROR. It gives that error on the elses that are on the far left. What is wrong?? (I use Eclipse) Code: package  Hello everyone! So i was trying to do an report plugin but it gives me the error "Syntax error on token "}", { expected after this token " on line 40,

"Syntax error on token "else", delete this token" ERROR : java, Syntax error on token ")", ; expected after this token. Posted 27 January 2016 - 05​:51 AM. Hi, I'm creating a program in Java using JOptionPane, my problem  You had the getDouble method inside the main method. This confuses the compiler. Also, the type (string) in the getDouble argument (string prompt) should be capitalized, viz, "String prompt".

Syntax Error On Token ")", ; Expected After This Token, Bugzilla – Bug 397115 Java Problem: Syntax error on token "Object", VariableDeclarator expected after this token Last modified: 2013-06-06 10:45:31 EDT. Nothing wrong with your import statement, in jsp page you can always have multiple imports. But something looks wrong with you class declarations, in a jsp page we can only declare abstract or final classes.

397115 – Java Problem: Syntax error on token "Object , Hi, We usually open the code tab and see if there has compile error in java line. What does your sql query look like on tMySqlInput component? We would like to show you a description here but the site won’t allow us.

Comments
  • Put code inside a method instead of directly inside class.
  • So what does that mean? I have to take the for-loop out and make a separate class for it?
  • @user Read what you just wrote and this statement in Juned's answer: you cannot directly write the executable statements in class.
  • @user2615699 check my updated answer. I have moved everhing to main method just for the sake of testing. You can move the variables at the class level if required.