I'm very new to java having done most of my scripting in Javascript and a little bit in other types of programs.
I'm basically trying to obtain a script variable and compare it against a condition and return some println messages. Here is my code:

    String agentType[] = request.getParameterValues("Agent Flag");
    if (agentType[] != null && agentType[].length != 0 ) {
    System.out.println("You have selected");
    else {
    System.out.println("You have not selected");

I'm getting the illegal start of type error message on the if line.

Any help will be greatly appreciated.

Edited 3 Years Ago by Dani: Fixed formatting

First of all, on the if statement you say agentType[] != null
What are you trying to do, if you are just checking the first element do this:

if(agentType[0] != null)

Now for the second part in the if statement. replace

agentType[].length !=0

To this:

agentType.length != -1

So in the end your if statement should look like this:

if (agentType[0] != null && agentType.length != 0 )
        System.out.println("You have selected");

actually, he's not trying to see whether the first element is (or is not) null, but he's trying to verify whether or not the array is null.

when declaring the array, you'll have to add the [] square brackets, but when using it afterwards, you just use the name of the variable:

if ( agentType != null && agentType.length != 0 ){
  System.out.println("You have selected");

the problem with godzab's solution: if the array IS null => you'll try to check the non-existing first element and check whether or not that is null.

Thanks guys for the replies. Here is the entire error message:

49: illegal start of type if (agentType != null && agentType.length != 0 ) {

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.