Question

I am having a problem when I am trying to compile a pattern program. I am creating this program in BLUEJ and when I am trying to compile it shows the error : "not a statement"

class pattern
{
    public static void main()
    {
        int p=0;
        for(int i=1;p=1;i<=4;i++,p++)
        {
            for(int j=1;j<=i;j++)
            {
                System.out.print(Math.pow(p,2);

            }
            System.out.println();

        }
    }
}

What is the problem?

Was it helpful?

Solution

A couple of issues there, the main one being this:

for(int i=1;p=1;i<=4;i++,p++)
//         ^   ^    ^

The for statement consists of three, not four, parts separated with ;. I suspect you wanted

for(int i=1,p=1;i<=4;i++,p++)
//         ^--- comma here

Separately, I believe you have to specify the argument to main even if you're not using it, so:

public static void main(String[] args)

In a comment on the qustion, cadrian pointed out a further problem:

System.out.print(Math.pow(p,2);
// Missing ) here ------------^
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top