File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes creating a method to get rid of a repeated code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating a method to get rid of a repeated code ?" Watch "creating a method to get rid of a repeated code ?" New topic
Author

creating a method to get rid of a repeated code ?

Adnan Al-Jehani
Greenhorn

Joined: Mar 02, 2012
Posts: 8

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

Joined: Oct 27, 2005
Posts: 19684
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Adnan Al-Jehani
Greenhorn

Joined: Mar 02, 2012
Posts: 8

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

Joined: Oct 13, 2005
Posts: 38517
    
  23
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

Joined: Mar 02, 2012
Posts: 8

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

Joined: Apr 06, 2010
Posts: 4376
    
    8

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

Joined: Mar 02, 2012
Posts: 8

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

Joined: Oct 13, 2005
Posts: 38517
    
  23
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating a method to get rid of a repeated code ?