• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Very weird illogical 'if statement' problem

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am working on a program that checks if two string are anagrams of each other. To those of you who don't know what an anagram is, it is simply two words in which you can rearrange the characters of one word to get the second word. Example: Dog, GdO.

My problem is that my program is picking up on 'Anagrams' even though it shouldn't and I stepped through my logic several time, and it makes complete sense to me.

More specifically, the if statements comparing one character to the other character are evaluating to true even though it shouldn't.

In other words I am 'asking' my program does 'g' == '4' and it says YES.

Note: I change the character of the wherever a match was found '4' (which is not a letter) that way, the next time the loop runs it wont pick up on the SAME match twice.

This is very frustrating, Any help will be greatly appreciated!

Thank you!


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post more code - Put this code in a context in which we can see where and when it is run, and can compile and run the code?

A couple of notes:


This is repetitive. If the first comparison is false then the second comparison must be true. So you should simplify the code, and make it just:



to avoid confusion and possible errors (less code is better. Duplication is bad.)

Your comparison for comparing case is not very good either. It doesn't take into affect non-cased characters. For example, if one character is '4' and the other is 'T' would pass the 'ignore case' comparison as equal. Perhaps a better approach would be to use the tools available in the Character class:
 
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your solution looks weird. I think you need to write down, in words of one syllable, how you are working out which words are anagrams of each other. Try it with three words (three there ether) and see if you can prove they are anagrams. Those three words once appeared in opposite corners of the Times crossword. There is somewhere in the Java™ Tutorials an exercise with a set of 11 anagrams, but I can't remember where.
Once you have worked out how to do it, then you can write the code.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A hint: consider an algorithm where you transform both words into another form, such that if those two other forms are equal, the words are anagrams. Such an algorithm would be vastly simpler than what you're trying to do here.
 
Greenhorn
Posts: 25
Netbeans IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I am comparing two strings, I use code similar to the following:



Oh....I see you are comparing characters....never mind
 
Campbell Ritchie
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't so much as compare those Strings as test them for equality.

But, as I said and Ernest agreed with, you need to work out the algorithm. There are much much simpler ways to test whether words are anagrams of each other than what you appear to be doing.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic