Identifier Expected Error. New to Java!

T

Tim

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" compilere error at line 5 (array = new boolean array[20];)

public class IntegerSet {

boolean[] array;

array = new boolean array[20];

IntegerSet() {
for(int i = 0; i < array.length; i++) {
array = 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 + " " + "]");
}
}
}

Thank u in advance.
 
B

Bjorn Abelli

...
I'd appreciate it if anyone could tell me
what's wrong with the following piece of code
boolean[] array;
array = new boolean array[20];

You cannot have a statement like the second line outside the scope of a
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.
and how to fix it.

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 + " " );
}
System.out.println("]");
}


// Bjorn A
 
T

Tim

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


Bjorn Abelli said:
...
I'd appreciate it if anyone could tell me
what's wrong with the following piece of code
boolean[] array;
array = new boolean array[20];

You cannot have a statement like the second line outside the scope of a
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.
and how to fix it.

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 + " " );
}
System.out.println("]");
}


// Bjorn A
 
Joined
May 20, 2008
Messages
1
Reaction score
0
instance variable initialization

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

Bjorn Abelli said:
"Tim" wrote...

> I'd appreciate it if anyone could tell me
> what's wrong with the following piece of code


> boolean[] array;
> array = new boolean array[20];


You cannot have a statement like the second line outside the scope of a
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.

> and how to fix it.


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 + " " );
}
System.out.println("]");
}


// Bjorn A
 
Joined
Jun 20, 2008
Messages
2
Reaction score
0
help...identifier expected!

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

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
 
Joined
Jun 20, 2008
Messages
2
Reaction score
0
Bjorn...

Hi Bjorn, could you help?

conteh said:
Im having the same problem. need help....

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
 
Joined
Aug 25, 2008
Messages
1
Reaction score
0
Similar problems

Hi, Ive had similar problems as well, I tried following Bjorn's advice but it did not seem to work. Here's how it was before:

public class MyString
{
int i=0;
private char[] data;

public MyString(int length, char value)//Constructor MyString doesn't work??
{
data = new char[length];

for(i=0; i< data.length; i++)
{
data = value;

}
}

public void print()
{
String array = new String(data);
System.out.println(array);
}

This enables me to print arrays of length 1, or if I enter length = 2, it only accepts one char value, else it returns Error: <identifier> expected. When I tried Bjorn's idea, it returned Null error in the print method.
Thanks for help
Chris
 
Joined
Dec 23, 2008
Messages
1
Reaction score
0
can somebody tell me whats wrong with this code? I am trying to make a program that seaches an unsorted list for a user input number that counts the number of comparisons and prints the number of comparisons.


import java.util.Scanner;
public class SerialSearch
{
public static int search (int[]numbers, intkey)
{
Scanner in = newScanner();
boolean found = false;
int i = 0;
while((i<numbers.length)
{
if number== key
{found = true;
return i;
}
i++

}
 
Joined
Mar 3, 2009
Messages
1
Reaction score
0
Problems with Java Expressions

Hi,

Im using an IBM product to move asset data from one DB to pop another.

Im using Integration Composer and building expression maps to assist with this process.

I am getting the following errors:-
D:\Integration Composer\genrules\MAX_ASSETS_V2_2537\CIT_Asset0.java:160: 'class' or 'interface' keyword expected.
public void set_siteid(CDeployed_Asset pDeployed_Asset)


Can any one see from my code below whats wrong:-

{
String itemnum = 'Deployed Asset.Nodename';
if(!isNull(itemnum)){
String site_ident = itemnum.substring(1, 4);

if ((site_ident.substring(1, 2) == "UK") || (site_ident.substring(1, 2) == "AU") || (site_ident.substring(1, 2) == "SI") || (site_ident.substring(1, 2) == "IE"))
{ if (site_ident.substring(3, 4) =="RH")
{return("EU02");
}
else if (site_ident.substring(3, 4) =="AS")
{return("EU00");
}
else if (site_ident.substring(3, 4) =="SI")
{return("AS03");
}
else if (site_ident.substring(3, 4) =="DU")
{return("EU03");
}
else if (site_ident.substring(3, 4) =="SA")
{return("EU07");
}
else if (site_ident.substring(3, 4) =="BI")
{return("EU28");
}
else if (site_ident.substring(3, 4) =="OS")
{return("EU14");
}
else if (site_ident.substring(3, 4) =="GS")
{return("EU08");
}
else if (site_ident.substring(3, 4) =="SY")
{return("AU02");
}
else if (site_ident.substring(3, 4) =="PP")
{return("EU15");
}
else if (site_ident.substring(3, 4) =="MU")
{return("EU02");
}
}
else {
if (site_ident.substring(2, 3) =="RH")
{return("EU02");
}
else if (site_ident.substring(2, 3) =="AS")
{return("EU00");
}
else if (site_ident.substring(2, 3) =="SI")
{return("AS03");
}
else if (site_ident.substring(2, 3) =="DU")
{return("EU03");
}
else if (site_ident.substring(2, 3) =="SA")
{return("EU07");
}
else if (site_ident.substring(2, 3) =="BI")
{return("EU28");
}
else if (site_ident.substring(2, 3) =="OS")
{return("EU14");
}
else if (site_ident.substring(2, 3) =="GS")
{return("EU08");
}
else if (site_ident.substring(2, 3) =="SY")
{return("AU02");
}
else if (site_ident.substring(2, 3) =="PP")
{return("EU15");
}
else if (site_ident.substring(2, 3) =="MU")
{return("EU02");
}
}

}


else
skipCurrentInstance();
}


}

Thanks

Steve
 
Joined
Dec 8, 2009
Messages
1
Reaction score
0
this happened to me......i was missing a method......when i wrote in my main method at the top

public static void main(String[]args)

{
}

then everything worked out.....hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top