This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing a String to NOT Equal  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There!
I'm taking a beginner's programming class. Last night's lab was to compare a String to a String array of vowels and if a character in our String matched a character in the String array of vowels then print out the matching vowels. Today I added an IF statement to check if a character in the String does NOT equal the array of vowels then to print out the character which does not match but that's not working. I can't figure out why it's not working. Can someone help me?


 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know about if then else? It looks like this:

Another hint: you only need one inner loop.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The entire content of main() can be easily replaced with less than 8 lines of code if you make vowels a String instead of an array of String's, and use String's indexOf() method.
(Actually, to include the printout of the number of vowels and consonants, I do it in 15 lines).
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Do you know about if then else? It looks like this:

Another hint: you only need one inner loop.


I did try that originally at first using else and else if but it was still outputing both vowels and constants . I'll look at trying that again. Thank You
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:The entire content of main() can be easily replaced with less than 8 lines of code if you make vowels a String instead of an array of String's, and use String's indexOf() method.
(Actually, to include the printout of the number of vowels and consonants, I do it in 15 lines).


I'll look at that. Thanks.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:. . . and use String's indexOf() method. . . ..
Why use indexOf when you can use contains?
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"contains" requires a String input whereas "indexOf" can use a char. My solution uses vowels as a String, not an array of Strings:
if ( vowels.indexOf( word.charAt( i ) ) >= 0 ) {...}
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually contains takes a CharSequence.
if (vowels.contains(String.valueOf(word.charAt(i)))...
if (vowels.contains(word.substring(i, i + 1)))...

I shall check substring that it will take that without throwing an out of bounds exception. I think that will be all right even for the last letter. I didn't find anything helpful in the Character class.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, contains takes a CharSequence.
But the constructs
keep creating lots of new String instances, whereas vowels.indexOf( word.charAt( i ) ) does not.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Favour legibility over performance. The Strings will probably be created on the stack because of escape analysis so there will be no GC overhead. And contains(...) is much easier to read than
if (word.indexOf(vowels.charAt(i)) > 0)...
At least that is what I think. Others may disagree.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Do you know about if then else? It looks like this:

Another hint: you only need one inner loop.


Hi Knute Snortum,

I need more information please if you do not mind and Thank you for your help.

Question about your suggestion with using the IF and Else statement.
I had tried using an "else" statement where I didn't have any false statement but only a System.out.println("Current constant is " + character); and that didn't work. I then moved to an "Else If" statement ( shown below ) which I was able to add a false statement and I still have the wrong output.



Also I am having trouble regarding your hint about needing only one inner loop. Can you please elaborate a bit on that?

It could just be I don't understand exactly what equates to one loop. I have a "For" and then an "IF" and then an "Else if ". So I count two inner loops inside the FOR statement. IF and the Else IF are inside the For. If I am counting this correctly then I currently have two inner loops? Does your hint mean that I need to figure out how to have JUST an IF and Else statement with no FOR?




Below is how I'm using the IF and ELSE IF statement . I thought that if I had an exclamation point it would read as

"else if not character.equals to vowels "

I know there are other ways to achieve the goal of this lesson and I want to explore those options as well but I want to learn what I'm doing wrong here first.

Thank You so much for your help.


 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you never need to write:

Simplify the code to

Next, think about what it means when this code evaluates false:

my_vowels.vowels[0] evaluates to "a". So if you find yourself in the else block, the letter is not an "a". That means it could be another vowel or a consonant, you don't know. Is the problem clearer now?

Then think about how you would solve this problem by explaining it to a five-year-old. Writing it down often helps. Then, only then, start coding again.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:First of all, you never need to write:

Simplify the code to

Next, think about what it means when this code evaluates false:

my_vowels.vowels[0] evaluates to "a". So if you find yourself in the else block, the letter is not an "a". That means it could be another vowel or a consonant, you don't know. Is the problem clearer now?

Then think about how you would solve this problem by explaining it to a five-year-old. Writing it down often helps. Then, only then, start coding again.



Okay bear with me. I had the "else" only previously and still it was printing vowels as constants . Here is what I had



So what I understand now is I need to re-evaluate my IF condition instead? Thank you for your help with this. I'll do what you suggested and try and think about what this codes reads and evaluates to.


 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That last post sounds as though you have to resort to desperate measures. Look at the back of your computer. Is there a black cable running from it to the wall? Remove that cable. That will work for a desktop. For a laptop you may have to make do with simply turning the machine off.
Then work out what you are trying to do on paper. You will never work out how to do it until you know what you are trying to do, and you will never manage that with the computer running.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:First of all, you never need to write:

Simplify the code to

Next, think about what it means when this code evaluates false:

my_vowels.vowels[0] evaluates to "a". So if you find yourself in the else block, the letter is not an "a". That means it could be another vowel or a consonant, you don't know. Is the problem clearer now?

Then think about how you would solve this problem by explaining it to a five-year-old. Writing it down often helps. Then, only then, start coding again.


Ignore most of what I said previously . I re-read and I'll re-evaluate based on what you said here
"That means it could be another vowel or a consonant, you don't know."
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That last post sounds as though you have to resort to desperate measures. Look at the back of your computer. Is there a black cable running from it to the wall? Remove that cable. That will work for a desktop. For a laptop you may have to make do with simply turning the machine off.
Then work out what you are trying to do on paper. You will never work out how to do it until you know what you are trying to do, and you will never manage that with the computer running.


Okay . I'm a frustrated about my lack of not understanding how these things read. My instructor does encourage writing "p-code" but I sort of feel that even when I do that, I'm not reading this information correctly. I *think* I do but I am not. I'll try and just write it out on paper instead as being suggested.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That last post sounds as though you have to resort to desperate measures. Look at the back of your computer. Is there a black cable running from it to the wall? Remove that cable. That will work for a desktop. For a laptop you may have to make do with simply turning the machine off.
Then work out what you are trying to do on paper. You will never work out how to do it until you know what you are trying to do, and you will never manage that with the computer running.


Oh Thank You for those two articles .
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa Austin wrote:Okay . I'm a frustrated about my lack of not understanding how these things read. My instructor does encourage writing "p-code" but I sort of feel that even when I do that, I'm not reading this information correctly. I *think* I do but I am not. I'll try and just write it out on paper instead as being suggested.

Posting your pseudocode -- or even instructions to a five-year-old -- will help you clarify your thought process.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how I would use charAt and indexO, using String instead of array of String for the vowels variable:

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Lisa Austin wrote:Okay . I'm a frustrated about my lack of not understanding how these things read. My instructor does encourage writing "p-code" but I sort of feel that even when I do that, I'm not reading this information correctly. I *think* I do but I am not. I'll try and just write it out on paper instead as being suggested.

Posting your pseudocode -- or even instructions to a five-year-old -- will help you clarify your thought process.


Gotcha. I had my pseudocode on the original post and it got flagged for being too long I didn't save it. I'll just try and do it again. I think even thought I had it's obvious it will help me to redo them.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Here's the content of my main() to solve this:




Thank You. Not looking at it yet LOL I appreciate the help. I think it's something I need to figure out myself first Then I'll compare what I did to what you did.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa Austin wrote: Thank You. Not looking at it yet LOL I appreciate the help. I think it's something I need to figure out myself first Then I'll compare what I did to what you did.

Good for you.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yesterday, I wrote:. . . if (word.indexOf(vowels.charAt(i)) > 0)... . . .
…and nobody has told me off for the out‑by‑error. Another reason for using contains; it has been checked for correctness and there is no risk of such out‑by‑one errors.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!