aspose file tools*
The moose likes Beginning Java and the fly likes methods problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "methods problem" Watch "methods problem" New topic
Author

methods problem

Dan Mejia
Greenhorn

Joined: Aug 18, 2011
Posts: 3
Hi, I started a Java training class 2 weeks ago. I know how to break a program into methods but the teacher gave us one to break that it is too advanced. I've tried many things but nothing works. Here is the original program to create methods from. I appreciate some help, not necessarily a solution but how to do it. Thanks.
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

Dan,

One possible solution - Refactor the class into 3 methods:
1) private void readUserInput
2) private void writeFile (File file)
3) private void readFile (File file)

Then have these methods called from main method.

Thanks,
Badal


Blog: http://badalchowdhary.wordpress.com/
Twitter: http://twitter.com/badalrocks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39413
    
  28
I would add to that: move the main method into a different class. You could call it DataReadDemo or something like that.
Dan Mejia
Greenhorn

Joined: Aug 18, 2011
Posts: 3
Badal, I had already done that but thanks anyway. Ritchie, I was planning to do that to. I think my problem is with passing the reading. Anyway, the teacher will explain it and I will see my errors. Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39413
    
  28
You realise you can use Scanner and Formatter to read and write text files? There is an example in the Java™ Tutorials. Make sure to look in the Formatter class for what all the hieroglyphics after the % character mean, but the tutorials link will probably give you a simplified list.
Dan Mejia
Greenhorn

Joined: Aug 18, 2011
Posts: 3
I was able to complete the assignment before the class started. Since my program needed some polishing the teacher suggested a few changes to make it more efficient. I am uploading it in case it helps somebody. Some parts of my original code were commented out. I took Badal's suggestions on the names, the ones I had selected for the methods were too weird. Ritchie, yes I know about Scanner, I prefer to use it for my own little programs but it is not used for the Java course I am taking. Thanks again.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: methods problem