Jump to content

About incompatible types error message and correction about the problem


anson920520

Recommended Posts

I currently need to finish a task for my project which needs me to create a Create a display component to show Select pet shop name and total Pets in that shop. For this tasks i created a several components,I had a function call "getPetsInPetShop" to do what i mention

	   public static ArrayList<Pet> getPetsInPetShop(Petshop petshopName)	{		// this should go through all pets and return only those whose		// pet shop name is the same as the parameter...				//new arrylist to store information about pets		ArrayList<Pet> result = new ArrayList<Pet>();				//loop thought the csv file		for (Pet p : pet)		{			//if the user input a petshop name that can be found in the csv file			if (p.getShop().equals(petshopName))			{				//return thouse pet p				result.add(p);			}		}		//Here is the output		return result;	}   

I assume this can allow me to loop thought how many pets i got inside the selected pet shop and return it. On the other hand this is my code to pass the function when the program run. This is mousevent function, when the user click on it , a new panel will appear for them to input information.

 public class SelectPetShopControl implements MouseListener{		 	@Override	public void mouseClicked(MouseEvent e)	{		 JLabel clicked = (JLabel)e.getSource();		 String PetShopName, TotalPets;		 SelectPetShopPanel spsp = (SelectPetShopPanel)clicked.getParent();		 PetShopName = JOptionPane.showInputDialog("Enter a pet shop name");		 if(PetShopName != null){	    TotalPets = ApplicationModel.getPetsInPetShop(TotalPets);			  spsp.setPetshop("Pet Shop Nmae: "+PetShopName);			  spsp.setTotalPets("Total: "+TotalPets);		 }	}

Everything in here is right expect "TotalPets = ApplicationModel.getPetsInPetShop(TotalPets);"The error message in here is incompatible types. I had look at some example and try to fix it by myself,but i realize i don't have enough technology to finish it. Many thanks

post-114906-0-26744600-1357298032_thumb.jpg

Link to comment
Share on other sites

the problem I see is in SelectPetShopControl, that you are declaring TotalPets as type String here

String PetShopName, TotalPets;

but then re-assigning its value with the return value of ApplicationModel.getPetsInPetShop

TotalPets = ApplicationModel.getPetsInPetShop(TotalPets);

but the function signature of ApplicationModel.getPetsInPetSho declares that it is going to return an ArrayList of Pet objects.

public static ArrayList<Pet> getPetsInPetShop(Petshop petshopName)

so I would create a variable for just the list and assign the return value to that, and make sure you are actually PetShopName to ApplicationModel.getPetsInPetShop

JLabel clicked = (JLabel)e.getSource();String PetShopName; SelectPetShopPanel spsp = (SelectPetShopPanel)clicked.getParent();PetShopName = JOptionPane.showInputDialog("Enter a pet shop name"); if(PetShopName != null){	    ArrayList<Pet> TotalPetsInShopList = ApplicationModel.getPetsInPetShop(PetShopName);  spsp.setPetshop("Pet Shop Nmae: "+PetShopName);  spsp.setTotalPets("Total: "+ TotalPetsInShopList.size());}

Edited by thescientist
Link to comment
Share on other sites

Very good. I didn't notice that ApplicationModel was the code fragment shown. The types don't match up in various places and that passed parameter should obviously be PetShopName, although that in itself seems a bit crude. Wouldn't you want the user to select from an enum of valid pet shop names?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...