6 Replies - 1263 Views - Last Post: 24 August 2013 - 07:13 AM Rate Topic: -----

#1 flaminsnowman99   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 15-December 12

How To Ignore Case of a String

Posted 23 August 2013 - 09:32 PM

Hello! So for the project I'm working on I have an enum class full of colors.
WHITE(0, "White"), RED(1, "Red")

It looks like that, and has lots of colors. Now, I'm trying to check to see if the input from the project is a color with a try/catch statement like this.
try {
	String color = Colors.valueOf(input).name();
	System.out.println(color);
} catch (Exception e) {
	System.out.println("Not a color.");
}

So that is how I have tried to do it, and it works, but not quite how I want. Because it is using the name() method, the input has to be RED, or WHITE. I want the input to be any case and still work.

Any help is greatly appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How To Ignore Case of a String

#2 Mylo   User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: How To Ignore Case of a String

Posted 23 August 2013 - 09:48 PM

There exists a function in the String class for this.
myString.equalsIgnoreCase("String to match.");

Edit:
I Reread your post and now thought you might mean something else. There is also a
myString.toUpperCase()

Personally I wouldn't use enums for such a problem, but a list of strings, which is a more flexible approach, and can be modified at run time.

This post has been edited by Mylo: 23 August 2013 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 flaminsnowman99   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 15-December 12

Re: How To Ignore Case of a String

Posted 23 August 2013 - 09:59 PM

The string is not being compared to anything though....

In this case where the string would be the input.

This post has been edited by flaminsnowman99: 23 August 2013 - 09:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 Mylo   User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: How To Ignore Case of a String

Posted 23 August 2013 - 10:09 PM

You don't need to compare, but you only need to get the string into a form which is the same of that of the enum. The enum name cannot be changed at run time. If you have an enum "DreamInCode", then the only way is to make sure that the input has a capital D, I, and C, you cannot ignore this.

You can do

			if (Color.RED.name.equalsIgnoreCase(input))
			{
				
			}


but this defeats the purpose of what is trying to be accomplished.

This is why I suggest to not use enums, but a list (or other data structure), of strings, of which you can modify at run time, and do a case insensitive comparison.

This post has been edited by Mylo: 23 August 2013 - 10:39 PM

Was This Post Helpful? 0
  • +
  • -

#5 flaminsnowman99   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 15-December 12

Re: How To Ignore Case of a String

Posted 23 August 2013 - 10:18 PM

I will consider switching too a list. I used enums because I just learned them and it seemed practical and I wanted to try it, haha.

A list will be much easier and save me the trouble of doing it that, way. Thank you for the help nonetheless!
Was This Post Helpful? 0
  • +
  • -

#6 Mylo   User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: How To Ignore Case of a String

Posted 23 August 2013 - 10:37 PM

Ok, if you're still here, I tried it out myself, there is another approach I didn't think about.

		String input = "rEd";
		for (Color c : Color.values())
		{
			if (input.equalsIgnoreCase(c.name))
			{ // or if (input.equalsIgnoreCase(c.name()))
				System.out.println(c.num);
			}
		}

This post has been edited by Mylo: 23 August 2013 - 10:43 PM

Was This Post Helpful? 1
  • +
  • -

#7 flaminsnowman99   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 15-December 12

Re: How To Ignore Case of a String

Posted 24 August 2013 - 07:13 AM

That's a great way of doing it! I didn't even think of doing that. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1