Hello all!

I am having a few issues with my code and after hours of endlessly searching Java help sites and Java forums, I decided to bring my issue to the table to receive some help specific to me, I am receiving the error 'Illegal start of expression' with the '{' underlined with red showing the error, I am relatively new to Java coding however I am not new to coding in general, your help in this matter is greatly appreciated all, the code is listed below and performs functions such as screenshotting, variable retrieving from the screenshots, mouse movement and clicking then finally the outputting of the variable stored.

package Mouseclickingprogram;

import java.awt.image.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import java.awt.Robot;
import java.awt.event.InputEvent;


public class mouseclicker {

    public static final String path_two = "C:/Pictures/Javascreenshots/";
	public static final String path = "C:/Pictures/Javascreenshots/";
	public static final String[] testtoclick = {"one","two","three","four","five"};
    public static final String[] clickamount = {"1","2","3","4","5"};
	
// Screen section dimensions
	public static final int TOP =  59;    // distance from top of screen to top of the trade screen
	public static final int BOTTOM = 685;  // distance from top of screen to bottom of the trade screen
	public static final int LEFT = 16;    // distance from left of screen to left of the trade screen
    public static final int RIGHT = 861;   // distance from the left of the screen to the right of the trade screen

    // click amount box

    public static final int BOX_WIDTH = 125;
    public static final int BOX_HEIGHT = 16;

     public void main(String[] args) 
    {
    
    // takes and saves a screenshot of the entire screen
	public static BufferedImage screenCapture(int click, int instance)
			throws HeadlessException, AWTException, IOException {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		BufferedImage image = new Robot().createScreenCapture(
				new Rectangle(dim));
		BufferedImage image2 = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
		image2.getGraphics().drawImage(image, 0, 0, null);
		ImageIO.write(image2, "png", new File(path_two + "scr" + click + "_" + instance + ".png"));
		image = ImageIO.read(new File(path_two + "scr" + click + "_" + instance + ".png"));
		return image;
	}
		
	static int[][] clickSamples = new int[clickamount.length][BOX_WIDTH * BOX_HEIGHT * 4];
	static int[] clickSample = new int[BOX_WIDTH * BOX_HEIGHT * 4];
	
	//finds the click amount number to enter once click has been made
	static int getClick(BufferedImage image, int x, int y) {
			image.getRaster().getPixels(x, y, BOX_WIDTH, BOX_HEIGHT, clickSample);
			for (int j = 0; j < clickamount.length; j++) {
				if (Arrays.equals(clickSample, clickSamples[j])) {
					return j;
				}
            }
            return -1;
            }

public static void mouseclick (String[] args) throws Exception{

    Robot r = new Robot();
    r.mouseMove(757,118);
    //Clicks on the screen
    r.mousePress( InputEvent.BUTTON1_MASK );
    r.mouseRelease( InputEvent.BUTTON1_MASK );
    //Inserts the number found from the screen shotsa
    System.out.println(clickamount.length);
}
}

I do hope you can provide me with some help and hopefully a solution.

Regards

Inf.

You have declared your "screenCapture()" method within your main() method, which is not allowed. You cannot declare functions within functions. Your main() declaration is missing the static modifier as well, which it needs.

Edit: Looking more closely, you actually have every method inside main() and no terminating brace for the class. Perhaps you deleted the closing brace on main() by mistake?

You have declared your "screenCapture()" method within your main() method, which is not allowed. You cannot declare functions within functions. Your main() declaration is missing the static modifier as well, which it needs.

Edit: Looking more closely, you actually have every method inside main() and no terminating brace for the class. Perhaps you deleted the closing brace on main() by mistake?

Thank you for responding so fast to my thread, I can see this mistake now but could you please suggest a way for me to fix the error too? how would I have to change the code in order for the code to work? what re-ordering would have to happen and where would it have to be re-ordered too?

Regards

Inf.

Make the changes that Ezzaral suggested, then re-post your code. You are more than likely just missing a closing brace somewhere. For every opening { brace, you need a closing } one.

Well it has been a busy week, I have had a chance to try and adapt the code I have added a closing '}' bracket that was missing, however with my limited Java knowledge I am struggling with the solution as per the methods inside methods issue, I have a feeling I need to declare the methods outside of the Main() method then when they are required just call them back in the Main() function, but like I said with my limited Java knowledge currently I am not sure how to do this, having said that, it might not be the solution if not could anyone suggest how to overcome this problem? Thank you all in advance.

-Infect

class Test {
  static void method1() {

  }

  static int method2(String s, double d) {
    return 1;
  } 

  public static void main(String [] args) {
       method1();
       method2("asd", 123);
       int i = method2("asd", 10.00);
  }
}
class Test {
  static void method1() {

  }

  static int method2(String s, double d) {
    return 1;
  } 

  public static void main(String [] args) {
       method1();
       method2("asd", 123);
       int i = method2("asd", 10.00);
  }
}

This was very helpful thank you, I do however have another question, I noticed the 'static int method2(String s, double d)' and the call later on 'method2("asd", 123)' I am trying to work out the relationship between the String s, double d and the "asd", 123 sections, is the first declaration saying 'these are the variable/integer types' and the second is saying 'these are the actual variables?

Try and run this:

class Test {

  static void method2(String s, double d) {
     System.out.println("The s argument is: " + s);
     System.out.println("The d argument is: " + d);
  } 

  public static void main(String [] args) {
       method2("asd", 123);
  }
}

void method2(String s, double d) The method2 doesn't return anything: (void) and it takes 2 argument. The first is a String and the second a double.

The values that you pass: method2("asd", 123); Will populate the arguments of the method. LOCALLY.

Meaning that this wrong:

class Test {

  static void method2(String s, double d) {
     System.out.println("The s argument is: " + s);
     System.out.println("The d argument is: " + d);
  } 

  public static void main(String [] args) {
       method2("asd", 123);

// The s exists only inside the method2
       System.out.println("The s argument is: " + s); 
  }
}

But this is right:
TRY and run it:

class Test {

  static void method2(String s, double d) {
     System.out.println("The s argument is: " + s);
     System.out.println("The d argument is: " + d);
  } 

  public static void main(String [] args) {
// Not the variable that "lives inside the method"
       String s = "XXX"; 

       method2("asd", 123);

     System.out.println("The s of the main(String [] args) is: " + s);
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.