473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Really simple comparision

If I have the variable choice being inputted by the user, how can I use a non case sensitive comparison?

Basically without doing this:
[PHP]
choice = "a";

if(choice == "a" || choice == "A")
{...}[/PHP]
Feb 18 '08 #1
7 1159
r035198x
13,262 8TB
If I have the variable choice being inputted by the user, how can I use a non case sensitive comparison?

Basically without doing this:
[PHP]
choice = "a";

if(choice == "a" || choice == "A")
{...}[/PHP]
Always use the .equals method for comparing strings in Java.
There's also an equalsIgnoreCase method.
Feb 18 '08 #2
hirak1984
316 100+
Always use the .equals method for comparing strings in Java.
There's also an equalsIgnoreCase method.
but in that case, you have to override the equals method of the Object class for creating a meaningful comparison.

If you use the default ".equals" method of the Object, you are geting the same "==" comparison only.
Feb 18 '08 #3
r035198x
13,262 8TB
but in that case, you have to override the equals method of the Object class for creating a meaningful comparison.
...
There's no need to override anything. Like I said in my last response there is an equals and an equalsIgnoreCase method.


If you use the default ".equals" method of the Object, you are geting the same "==" comparison only.
That is not true. == compares equality of object references.
Try it with some code and give two different references the same string value. == will be false but .equals will be true.
Read about it in Sun's Java tutorial.
Feb 18 '08 #4
hirak1984
316 100+
That is not true. == compares equality of object references.
Try it with some code and give two different references the same string value. == will be false but .equals will be true.
Read about it in Sun's Java tutorial.
well I tried something like the following:
Expand|Select|Wrap|Line Numbers
  1.  
  2.         String one = "abc";
  3.         String two = "abc";
  4.         System.out.println(one==two);
  5.         System.out.println(one.equals(two));
  6.         System.out.println(one=="abc");
  7.         System.out.println(one.equals("abc"));
  8.         System.out.println(two=="abc");
  9.         System.out.println(two.equals("abc"));
  10.  

and all resulted "true"
in my eclipse.
can you mention, in which case, "==" and ".equals" will return different result?
Feb 18 '08 #5
JosAH
11,448 Expert 8TB
Change your first line to:

Expand|Select|Wrap|Line Numbers
  1. String one= new String("abc");
  2.  
... and run your code again.

kind regards,

Jos
Feb 18 '08 #6
Nepomuk
3,112 Expert 2GB
Back to the original question, you could use the String.toLowerCase (or String.toUpperCase) method.
Example:
Expand|Select|Wrap|Line Numbers
  1. String one = new String("abc");
  2. String two = new String("ABC");
  3. if(one.toLowerCase.equals(two.toLowerCase))
  4. {
  5.    // It will come here, as both are "abc" in lower case
  6. }
  7.  
Greetings,
Nepomuk
Feb 18 '08 #7
BigDaddyLH
1,216 Expert 1GB
Back to the original question, you could use the String.toLowerCase (or String.toUpperCase) method.
Example:
Expand|Select|Wrap|Line Numbers
  1. String one = new String("abc");
  2. String two = new String("ABC");
  3. if(one.toLowerCase.equals(two.toLowerCase))
  4. {
  5.    // It will come here, as both are "abc" in lower case
  6. }
  7.  
Greetings,
Nepomuk
method equalsIngoreCase combines that into a single method.
Feb 18 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Florian Lindner | last post by:
Hello, I've read the chapter in the Python documentation, but I'm interested in a a more in-depth comparision. Especially regarding how pythonic it is and how well it performs and looks under...
2
by: Ashwin Kambli | last post by:
Hi, I am doing a study on the performance comparision of C# and Java. Links to any articles on this topic will be greatly appreciated. Thanking you, Ashwin
3
by: kd | last post by:
Hi All, How to perform case-insensitive comparision of strings? Would there be some kind of an indicator, which when set to true, would allow case-insenitive comparision of strings using...
2
by: I Don't Like Spam | last post by:
I know this should be simple but I can't find it. Dim A as new object Dim B as object B = A Do Bunch of stuff Check if B still = A
2
by: Bernd Hohmann | last post by:
Dear collegues, small query problem. A table (simplified example)... customer as char(5) inv_date as date amount as double
2
by: nirav.lulla | last post by:
I have been given the task to come up with Requirements, Comparision and Migration document from Shadow Direct to DB2 Connect. I am very new much to all this, but kind of know a little bit about...
3
by: abctech | last post by:
I have an Html page, user enters a Date (dd-mm-yyyy) here. There's a servlet connected in the backend for processing this submitted information, it must have a method to compare this entered date...
1
by: srikanthch3 | last post by:
Hi I had problem in comparision of the start date and end date vba in excell.if u give the end date 2/2/07 and start date 3/2/07, it is accepting.so any one can help. Srikanth
1
kirubagari
by: kirubagari | last post by:
I Would Like Ask Ques On How To Open Bonding.bin File In C++ Or In Visual Basic 6.actually The Data In The Binary File Have Been Corrupted And I Would Like To Know The Method How To Open The File...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.