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

methods problem

 
Dan Mejia
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic