0

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 by Duki: n/a

4
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by peter_budo
0

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.

0

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

0

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

0

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 by peter_budo: Merge

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.