Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Identifier Expected Error in JAVA

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 25-10-2008
Member
 
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.

Reply With Quote
  #2  
Old 25-10-2008
Member
 
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.
Reply With Quote
  #3  
Old 25-10-2008
Member
 
Join Date: Jan 2008
Posts: 1,515
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("]");
}
Reply With Quote
  #4  
Old 25-10-2008
Member
 
Join Date: May 2008
Posts: 2,007
Re: Identifier Expected Error in JAVA

get rid of the 2nd "array":

array = new boolean[20];
Reply With Quote
  #5  
Old 25-10-2008
Member
 
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!
Reply With Quote
  #6  
Old 25-10-2008
Member
 
Join Date: Oct 2005
Posts: 2,389
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>
Reply With Quote
  #7  
Old 25-10-2008
Member
 
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.
Reply With Quote
  #8  
Old 25-10-2008
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Identifier Expected Error in JAVA"
Thread Thread Starter Forum Replies Last Post
<identifier> expected error mbueling Software Development 2 18-10-2011 07:04 AM
IDD_DIALOG1 error C2065 stating undeclared identifier Edi Software Development 5 26-06-2011 07:53 AM
<Identifier> Expected Error Circular Linked List Java JGriff254 Software Development 1 23-03-2010 03:25 PM
"Identifier Expected" error in Dr Java adam.taylor Software Development 5 12-05-2009 12:55 PM


All times are GMT +5.5. The time now is 08:27 AM.