Can someone explain why, when I input 'mytag' it does find it? When I debug, it shows the text matches, but the IDs are different. Not sure why that would matter?

public void RemoveTag() throws IOException, NoSuchTagException{
		
		System.out.printf("Tag to delete:  ");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		
		String tempTagText = reader.readLine();
		
			for (int i = 0 ; i < tags.size() ; i++)
			{
				if (tempTagText == tags.get(i).getTagName().toString())
				{
					tags.remove(i);
					System.out.println("Tag " + tempTagText + " removed.");
					return;
				}
			}
			throw
				new NoSuchTagException("The entered text does not match any tag");
	}
}
package main;

public class Tag {
	
	public Tag(String name) {
		tagName = name;
		tagID = nextTagID;
		nextTagID++;
	};
	
	private static int nextTagID = 0;

	public void setTagName(String name)
	{
		tagName = name;
	}
	
	public String getTagName()
	{
		return tagName;
	}
	
	public String tagName;
	public int tagID;
	
}

Recommended Answers

All 4 Replies

the IDs are different

What are the "IDs"?

tempTagText == tags.get(i).getTagName().toString()

Use the equals() method to compare the contents of String objects.

What class is the Equal() method in? I've never seen it.
Why are you posting code that does not exist?

What class is the Equal() method in? I've never seen it.
Why are you posting code that does not exist?

sorry, ment to 'up' your answer by one

sorry, ment to 'up' your answer by one

Equalized :icon_wink:

@Duki the "==" is for primitive types, for anything else either you will use provided equal method as NormR1 mentioned or you will write your own one including the hash method

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.