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.
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.
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("]");
}
Re: Identifier Expected Error in JAVA
get rid of the 2nd "array":
array = new boolean[20];
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!
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>
Re: Identifier Expected Error in JAVA
Quote:
Originally Posted by
absolute55
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.
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