Results 1 to 8 of 8

Thread: Identifier Expected Error in JAVA

  1. #1
    Join Date
    Oct 2008
    Posts
    47

    Identifier Expected Error in JAVA

    Hello all,

    I'd appreciate it if anyone could tell me what's wrong with the following piece of code and how to fix it. I get an "Identifier expected" compiler error at line 5 (array = new boolean array[20]

    Code:
    public class IntegerSet {
    
    boolean[] array;
    
    array = new boolean array[20];
    
    IntegerSet() {
    for(int i = 0; i < array.length; i++) {
    array[i] = false;
    }
    }
    
    public static void main(String[] arg) {
    System.out.println( "Values of the array: [ ");
    for (int i = 0; i < array.length; i++) {
    System.out.println(array[i] + " " + "]");
    }
    }
    }
    Thank u in advance.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Identifier Expected Error in JAVA

    You cannot have a statement like the second line outside the scope of method. Instance variables have to be either initialized at the point of declaration or assigned *inside* a method.

    Besides, don't use the variable name again on the right side of the assignment.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Identifier Expected Error in JAVA

    How to fix it...

    Code:
    boolean[] array = new boolean[20];
    
    ....or inside a method (or the constructor):
    
    
    boolean[] array;
    
    IntegerSet()
    {
    array = new boolean[20];
    ...
    }
    
    There is also another error in your code, as you try to access an instance
    variable from the static scope of main.
    
    Instance variables can only be used in the context of the instance. Either
    change the variable to be static, and use it in static context, or create an
    instance.
    
    public static void main(String[] arg)
    {
    IntegerSet s = new IntegerSet();
    System.out.print( "Values of the array: [ ");
    for (int i = 0; i < s.array.length; i++)
    {
    System.out.print( s.array[i] + " " );
    }
    System.out.println("]");
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Identifier Expected Error in JAVA

    get rid of the 2nd "array":

    array = new boolean[20];

  5. #5
    Join Date
    Oct 2008
    Posts
    47

    Re: Identifier Expected Error in JAVA

    Thank you both so much for your input and time you gave me! Especially you absolute55 helped me understand some important things about the syntax and use of the language! Thanks again!

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Identifier Expected Error in JAVA

    Those new to Java would get best help from.. <http://groups.google.com.au/groups?group=comp.lang.java.help>

  7. #7
    Join Date
    Apr 2008
    Posts
    60

    Re: Identifier Expected Error in JAVA

    Quote Originally Posted by absolute55 View Post
    You cannot have a statement like the second line outside the scope of method. Instance variables have to be either initialized at the point of declaration or assigned *inside* a method.

    Besides, don't use the variable name again on the right side of the assignment.
    Hi friend,
    can you please tell me the reason behind this. why can't we initialize instance variable in two statements.
    ex:int a;
    a=10;

    Is there any reason or simply java has specified like that and we should go accordingly. for me the above one seems to be absolutely logical.

  8. #8
    Join Date
    Oct 2008
    Posts
    79

    Re: Identifier Expected Error in JAVA

    Im having the same problem. need help....

    Code:
    public class WriteAMessage
    {
    public static void main(String []args)
    {
    if(null == args || args.length < 1) 
    {
    System.out.println("No message to print. Sorry.");
    System.exit(1);
    }
    
    }//end the if block
    
    String message = new String();
    
    //String message = formatArguments(args);
    
    message += formatArguments(args);
    System.out.println(message);
    
    }//end of the main method
    
    public static String formatArguments(String msg[])
    {
    String myArgs =new String();
    for(int i=0 ; i < msg.length ; i++)
    {
    myArgs
    
    += msg[ i ] + " ";
    }//end the for statement
    
    return myArgs;
    }//end formatArguments method
    //}//end the class

Similar Threads

  1. <identifier> expected error
    By mbueling in forum Software Development
    Replies: 2
    Last Post: 18-10-2011, 06:04 AM
  2. IDD_DIALOG1 error C2065 stating undeclared identifier
    By Edi in forum Software Development
    Replies: 5
    Last Post: 26-06-2011, 06:53 AM
  3. <Identifier> Expected Error Circular Linked List Java
    By JGriff254 in forum Software Development
    Replies: 1
    Last Post: 23-03-2010, 02:25 PM
  4. "Identifier Expected" error in Dr Java
    By adam.taylor in forum Software Development
    Replies: 5
    Last Post: 12-05-2009, 11:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,614,753.54967 seconds with 17 queries