Results 1 to 8 of 8
  1. #1
    madnbri is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default error: <identifier> expected

    Hi,
    I'm learning java there are something wrong at generics in my code. But where? It based on a very single sample.
    This is written in my ListExample.java text file:
    Java Code:
    import java.util.*; // I need java.util.List interface and java.util.ArrayList class
    public class ListExample {
       List<String> l = new ArrayList<String>();
       l.add("hello");
       String s = l.get(0);
       public static void main(String[] args) {
         System.out.println(s);
       }
    }
    It looks everything is ok, but:
    Java Code:
    javac ListExample.java 
    ListExample.java:4: error: <identifier> expected
    	l.add("hello");
    	     ^
    ListExample.java:4: error: illegal start of type
    	l.add("hello");
    	      ^
    2 errors
    Completely strange. Where is the mistake?

    Thanks in advance,
    m

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: error: <identifier> expected

    The single letter variable name makes the code hard to read. l vs 1

    Those statements need to be inside of a method,
    Last edited by Norm; 02-19-2014 at 03:10 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: error: <identifier> expected

    Wow Wow Wow take break and then check your code again..
    first remember where we declare variables and where we make common calculations or whatever..

    You have create a Public Class and the you have delcare the "l" as a List then you start to add elements inside "l" but not inside a method or in contructor but on "Variables Declaration Area" (as i like to call it , if i may) so the java understand that the line 4 is a Variable and that's why ask you to give the identifier (like : int , String ,boolean , float ...)

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: error: <identifier> expected

    Quote Originally Posted by ShadowWalker View Post
    Wow Wow Wow take break and then check your code again..
    first remember where we declare variables and where we make common calculations or whatever..

    You have create a Public Class and the you have delcare the "l" as a List then you start to add elements inside "l" but not inside a method or in contructor but on "Variables Declaration Area" (as i like to call it , if i may) so the java understand that the line 4 is a Variable and that's why ask you to give the identifier (like : int , String ,boolean , float ...)
    So basically what Norm said: "Those statements need to be inside of a method".
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    madnbri is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: error: <identifier> expected

    OK
    I need help.
    Those statements need to be inside of a method
    .
    Can somebody post this code?

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: error: <identifier> expected

    Quote Originally Posted by madnbri View Post
    OK
    I need help. .
    Can somebody post this code?
    You know how to write a method? Yes, because you made one called main. The main method, like all methods, are blocks of code between { and }. If anything has to be inside a method, put it between { and }.

    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    madnbri is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: error: <identifier> expected

    I've restructured a bit (not so bit) the original sample and works. Where can be known from about tutorial: it is incorrect? I have to check all read example. :(

    Thanks for answers.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: error: <identifier> expected

    Just to clarify, the { and } part is a code block. Methods have a special format which you have to use. From the tutorial:
    More generally, method declarations have six components, in order:
    1 Modifiers—such as public, private, and others you will learn about later.
    2 The return type—the data type of the value returned by the method, or void if the method does not return a value.
    3 The method name—the rules for field names apply to method names as well, but the convention is a little different.
    4 The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
    5 An exception list—to be discussed later.
    6 The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.
    You want your code in part 6.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. <identifier expected> error
    By jacjacjac in forum New To Java
    Replies: 5
    Last Post: 10-14-2012, 03:39 AM
  2. identifier expected error
    By Java Learner in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:13 PM
  3. Error:identifier expected(Help!)
    By chhoton in forum New To Java
    Replies: 8
    Last Post: 09-22-2009, 05:42 PM
  4. Identifier expected error
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 06:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •