Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating a method to get rid of a repeated code ?

 
Adnan Al-Jehani
Greenhorn
Posts: 8
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Guys I was trying something out, I got my code to work 100% perfectly, but I wanna know how can I make it more elegant. So I thought why not instead of repeating same the 2 lines code three times (in case the user entered "Yes") why not just call a method which will do the same job ? and that's where I got stuck !

I guess I'm good in writing the main() method only, but when it comes to combining other methods with each others and calling this method from there and stuff I pretty much don't know what to do : (
So this is my code, pretty simple stuff, just trying to practice on different things like loops and if statements, anyways please advise ..

And also, is there anyway to make the equals() method consider the "yes, Yes & YES" all the same without detailing them as I did ? if there is a ways, is it suitable for me as a beginner to learn it at this stage or should I just leave it for later on ?




Waiting for your valuable input people : )





RegardS,
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.String has two methods for checking if another String is equal to it. One is equals(Object) which you're using already. Check the other one.
 
Adnan Al-Jehani
Greenhorn
Posts: 8
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:java.lang.String has two methods for checking if another String is equal to it. One is equals(Object) which you're using already. Check the other one.


Thanks for the tip but I'm still unable to figure it out since I couldn't fins a clear syntax to help me in this case !
I tried the following :




but I know for sure that I'm not doing it right, so anyone can help with this ? please what about creating a method to get rid or repeating the same lines over and over again ?






Thanks,
 
Campbell Ritchie
Sheriff
Pie
Posts: 49770
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you are not doing it right at all. I am surprised if you got that to compile at all. You were told to look for equals methods, so why did you go for compareTo? Back to the String class documentation, find the methods summary, and have another look.
 
Adnan Al-Jehani
Greenhorn
Posts: 8
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, you are not doing it right at all. I am surprised if you got that to compile at all. You were told to look for equals methods, so why did you go for compareTo? Back to the String class documentation, find the methods summary, and have another look.


Well, who said I was able to compile it ? lol !
Anyways I got it to work now, when rob told me to look for other methods, my eyes fell on the compareToIgnoreCase method and from the sound of it I thought this is it. Apparently I was wrong and I was supposed to use equalToIgnoreCase which I didn't know about until you gave me that link. So thank you guys : ).
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compareToIgnoreCase would have worked. But it returns an integer, not a boolean. So you'd have used it like this:
But equalToIgnoreCase is the right tool for this job.
 
Adnan Al-Jehani
Greenhorn
Posts: 8
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:compareToIgnoreCase would have worked. But it returns an integer, not a boolean. So you'd have used it like this:
But equalToIgnoreCase is the right tool for this job.


Interesting, well thanks for your input, this might become helpful sometime .

Thanks,
 
Campbell Ritchie
Sheriff
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic