Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

String Comparison

 
Ranch Hand
Posts: 41
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to check whether a string is palindrome or not ,but equals method not working correctly.



Expected result : true
Actual Result : false

Any idea what i am doing wrong?How to get expected result?
 
Rancher
Posts: 4177
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That unicode doesn't reverse the string.
Display mechanisms that understand that unicode character will display succeeding text in reverse, but that is not the case inside your code.
The String.equals() method will simply check the characters, the first of which won't, match.
 
Shubham Rai
Ranch Hand
Posts: 41
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:That unicode doesn't reverse the string.
Display mechanisms that understand that unicode character will display succeeding text in reverse, but that is not the case inside your code.
The String.equals() method will simply check the characters, the first of which won't, match.



If unicode doesn't reverse the sting then,how in reference  to above code is printing string in reverse order.How the string j is holding the value of string s in reverse order?
 
Marshal
Posts: 6960
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add print statements:


However, I need to admit, this is the first time I see something like that.
 
Liutauras Vilda
Marshal
Posts: 6960
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, Dave already explained. Here mode details about that unicode. So it seems it is treated as an instruction during the print.
 
Dave Tolls
Rancher
Posts: 4177
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shubham Rai wrote:
If unicode doesn't reverse the sting then,how in reference  to above code is printing string in reverse order.How the string j is holding the value of string s in reverse order?



As I said, that is handled by the output.
The unicode is an instruction to whatever outputs the String to do it backwards.

The String itself is unchanged.


You'll get something like:
Forwards=
sometext
Backwards=
txetemos


That space at the front of the "Forwards" output is the RTL unicode character.
 
Dave Tolls
Rancher
Posts: 4177
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, scratch that, replace line 5 with:
System.out.print(s.charAt(i));

Knew I shouldn't have relied on memory...

That gives the output such that the RTL is not on the same line as the text...
Otherwise it reverses it!


So that gives:

s
o
m
e
t
e
x
t


That first blank line is the RTL character.
 
Shubham Rai
Ranch Hand
Posts: 41
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave Tolls!
 
Marshal
Posts: 65037
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, I know I once won a competition by printing “nun” backwards, but how can you tell whether “madam” is printed backwards or forwards? When I tried it on JShell, or JDK12.0.2 (Fedora 30 x64) with non‑palindromes, it printed the text the same with and without the right‑to‑left character (or \u202a).
You can get some interesting effects on the Ranch with the \u202e‮ character embedded in the text.
 
Bartender
Posts: 2325
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This character has been stared in jokes:  https://m.xkcd.com/1137/

There have been hacks in the past where someone would get a file called "document[202e character here]fdp.exe" and they would see "documentexe.pdf" and feel it's safe to open
 
Pay attention! Tiny ad!
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!