I wrote this code for a small project, and I am probably declaring a method in a method, because I keep getting "illegal start of an expression." This is happening at line 27, and 43. I am fairly new to java, so please don't be to harsh. I might have more errors in this code than I think. Thanks

// Loop with methods project
import acm.program.*;
import acm.graphics.*;
import java.awt.*;

public class MethodProject extends GraphicsProgram
{
  public static void main(String [] args) {new MethodProject().start(args);};
  public void run( )
  {
    singleLoop(0,0,10,20,30,40, Color.BLUE, Color.GREEN); //Controls placement, size, color
    doubleLoop(50,50,100,100,15,1);
    whileLoop(100,100, 0,70, 150, 200, Color.BLUE, Color.YELLOW);
  }
  public void singleLoop(int x, int y, int n, int sz, int dx, int dy, Color b, Color c) 
  {
    for (int x = 0; x <= 0; ball.move(1,1)) {
      GOval ball = new GOval(sz, sz);
      ball.setColor(b);
      ball.setFilled(true);
      add(ball,x,y);

      setBackground(Color.c);
      removeAll();
      pause(10000);
    }

    public void doubleLoop(int x, int y, int n, int sz, int dx, int dy) 
    {
      for (int x = 0; x < 700; x = x + dx) {
        for (int y = 0; y < 700; y = y + dy) {

          GRect rect = new GRect(x, y, sz, sz);
          rect.setFilled(true);
          ball.setColor(Color.RED);
          add(rect);
          pause(50);

          setBackground(Color.BLUE);
          removeAll();
          pause(10000);
        }

        public void whileLoop(int x, int y, int n, int sz, Color c, Color d) 
        {
          n = 0;
          GOval ball2 = new GOval(x, y, sz, sz);
          ball2.setFilled(true);
          ball2.setColor(Color.d);
          add(ball);

          GOval ball3 = new GOval(y,x, sz, sz);
          ball3.setColor(Color.c);
          ball3.setFilled(true);
          add(ball3);
          while (n < 20) {
            ball2.move(4,4);
            ball3.move(-4,4);
            pause(30);
            n++;
          }
        }
      }
    }
  }
}

Edited 3 Years Ago by Dani: Formatting fixed

Repost in code tags:

[code=JAVA] // code goes here

[/code]

Without them, we can't see the indentation and we have no idea what lines 27 and 43 are.

thank you

// Loop with methods project
import acm.program.*;
import acm.graphics.*;
import java.awt.*;

public class MethodProject extends GraphicsProgram
{
  public static void main(String [] args) {new MethodProject().start(args);};
  public void run( )
  {
    singleLoop(0,0,10,20,30,40, Color.BLUE, Color.GREEN); //Controls placement, size, color
    doubleLoop(50,50,100,100,15,1);
    whileLoop(100,100, 0,70, 150, 200, Color.BLUE, Color.YELLOW);
  }
  public void singleLoop(int x, int y, int n, int sz, int dx, int dy, Color b, Color c) 
  {
    for (int x = 0; x <= 0; ball.move(1,1)) {
      GOval ball = new GOval(sz, sz);
      ball.setColor(b);
      ball.setFilled(true);
      add(ball,x,y);
      
      setBackground(Color.c);
      removeAll();
      pause(10000);
    }
    
    public void doubleLoop(int x, int y, int n, int sz, int dx, int dy) 
    {
      for (int x = 0; x < 700; x = x + dx) {
        for (int y = 0; y < 700; y = y + dy) {
          
          GRect rect = new GRect(x, y, sz, sz);
          rect.setFilled(true);
          ball.setColor(Color.RED);
          add(rect);
          pause(50);
          
          setBackground(Color.BLUE);
          removeAll();
          pause(10000);
        }
        
        public void whileLoop(int x, int y, int n, int sz, Color c, Color d) 
        {
          n = 0;
          GOval ball2 = new GOval(x, y, sz, sz);
          ball2.setFilled(true);
          ball2.setColor(Color.d);
          add(ball);
          
          GOval ball3 = new GOval(y,x, sz, sz);
          ball3.setColor(Color.c);
          ball3.setFilled(true);
          add(ball3);
          while (n < 20) {
            ball2.move(4,4);
            ball3.move(-4,4);
            pause(30);
            n++;
          }
        }
      }
    }
  }
}

What's "acm"? Is that some library? Lines 27 and 43 are blank. What line is the error on? Actually it's fairly obvious. You have nested functions. You don't want that. Count your brackets. Every function needs to have the same number of starting brackets as it has ending brackets. You shouldn't have any functions within other functions.

Yes it is a library, umm line 28 and 44, my bad. Thank you for your post, but could you show me? I'm sorry, I just keep getting that illegal start of expression on those lines. I'm trying to call multiple methods.

thank you

// Loop with methods project
import acm.program.*;
import acm.graphics.*;
import java.awt.*;

public class MethodProject extends GraphicsProgram
{
  public static void main(String [] args) {new MethodProject().start(args);};
  public void run( )
  {
    singleLoop(0,0,10,20,30,40, Color.BLUE, Color.GREEN); //Controls placement, size, color
    doubleLoop(50,50,100,100,15,1);
    whileLoop(100,100, 0,70, 150, 200, Color.BLUE, Color.YELLOW);
  }
  public void singleLoop(int x, int y, int n, int sz, int dx, int dy, Color b, Color c) 
  {
    for (int x = 0; x <= 0; ball.move(1,1)) {
      GOval ball = new GOval(sz, sz);
      ball.setColor(b);
      ball.setFilled(true);
      add(ball,x,y);
      
      setBackground(Color.c);
      removeAll();
      pause(10000);
    }
    
    public void doubleLoop(int x, int y, int n, int sz, int dx, int dy) 
    {
      for (int x = 0; x < 700; x = x + dx) {
        for (int y = 0; y < 700; y = y + dy) {
          
          GRect rect = new GRect(x, y, sz, sz);
          rect.setFilled(true);
          ball.setColor(Color.RED);
          add(rect);
          pause(50);
          
          setBackground(Color.BLUE);
          removeAll();
          pause(10000);
        }
        
        public void whileLoop(int x, int y, int n, int sz, Color c, Color d) 
        {
          n = 0;
          GOval ball2 = new GOval(x, y, sz, sz);
          ball2.setFilled(true);
          ball2.setColor(Color.d);
          add(ball);
          
          GOval ball3 = new GOval(y,x, sz, sz);
          ball3.setColor(Color.c);
          ball3.setFilled(true);
          add(ball3);
          while (n < 20) {
            ball2.move(4,4);
            ball3.move(-4,4);
            pause(30);
            n++;
          }
        }
      }
    }
  }
}

Line 16 - starting bracket for function.
line 17 - starting bracket for loop.
line 26 - ending bracket.

Two starting brackets, one ending bracket. Need one more ending bracket before start of next function to even things out. Stick an ending bracket at line 27 to end the function.

Line 28 - start of new function.
Line 29 - starting bracket.
Line 30 - starting bracket.
line 31 - starting bracket.
Line 42 - ending bracket. Need two more ending brackets before start of next function. Stick two ending brackets on line 43.

Line 44 - Last function.

Start deleting end brackets starting on line 66 and working up until there are the same total number of starting and ending brackets in the overall code. Recompile after deleting each one till (hopefully) it compiles. If it doesn't, then you have other problems.

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