• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

methods problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 34
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic