public void mouseClicked(MouseEvent e) {
		store clickedStore = null;
			for (int i = 0; i < stores.length; i++) {
			if (stores[i].isClicked(e.getX(), e.getY())) {
				clickedStore = stores[i];
				break;
			}
		}
	}

Is it possible to display the result of the above loop as a string? I have tried various options but none have worked. It always results in the fact that a custom class cannot be converted to a string.

I have created my own class as shown below.

class store{
		String name;
		int x, y, width, height, centerX, centerY;
	
		public store(String storeName, 
				 	 int storeX, 
				 	 int storeY, 
					 int storeWidth, 
					 int storeHeight,
					 int storeCenterX,
					 int storeCenterY) {
				 
										name = storeName;
										x = storeX;
										y = storeY;
										width = storeWidth;
										height = storeHeight;
										centerX = storeCenterX;
										centerY = storeCenterY;
		}

store Asda = new store("Asda",100,100,40,40,120,120);
store Tesco = new store("Tesco",200,200,40,40,220,220);
store Morrisons = new store("Morrisons",400,400,40,40,220,220);
	
store[] stores = {Asda,Tesco,Morrisons};

I would like to display the result of the store I click on which is what the program is made for, however I cannot display the result of the aforementioned loop.

PLEASE HELP!

Hey, you could do this. If you add a toString method in your Store class, you should be able to display the clickedStore in a string format.

toString enables a class to "set" how the results will be returned.

when you go System.out.println(yo) in the background, it automatically calls upon the .toString() method of whatever data type yo is.

try this on your store class.

public String toString(){
                    
                    
                    return String.format("%s", name);
                }

Then, on your mouse event, try this.

for (int i = 0; i < stores.length; i++) {
			
				clickedStore = stores[i];
System.out.println(clickedStore);
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.