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;
	
}

Edited 5 Years Ago by Duki: n/a

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

Edited 5 Years Ago by peter_budo: Merge

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