View Single Post
Join Date: May 2007
Posts: 4,478
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: please enlighten me on what i need to do to get this right

 
0
  #8
Sep 29th, 2008
Originally Posted by SyLk View Post
I really would like to get this right and understand it as all my projects from here on are going to require me to manipulate data from a single file as input for the methods which will all be called from the the main(). This is what the instructor requires.

Whether or not its best practice is a totally different topic.
Actually, "best practice" doesn't really come into it. It's the intended usage of the language. Making everything static and manipulating it in main() could only be described as "unintended practice" or more appropriately "worst practice". Java is entirely an object-oriented language. To teach it otherwise is a disservice to students.

Your code can easily be converted to an appropriate object-oriented version with a few simple changes. The only static method that is needed here is main(). Any others can be normal public methods of the class. All variables needed internally by the class should be declared private. The static main() method simply serves as entry point or driver to "set up" and use the object that you create, like so
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5.  
  6. /** Not really a good descriptive class name, but I don't know
  7.  * what you're planning to do with it. You can pick a better one.
  8.  */
  9. public class FileProcessor {
  10.  
  11. private Scanner filescan = null;
  12.  
  13. /** Create an instance of FileProcessor for the given File. */
  14. public FileProcessor(File targetFile) throws FileNotFoundException {
  15. filescan = new Scanner(targetFile);
  16. }
  17.  
  18. /** Processing operations are carried out by calling
  19.   * public methods on an instance of this class.
  20.   */
  21. public void printFile() throws IOException {
  22. while (filescan.hasNextInt()) {
  23. System.out.println(filescan.nextInt());
  24. }
  25. }
  26.  
  27. /** main() can be used as a "driver" to manipulate an instance
  28.   * of this class. It should not do anything directly with the internal
  29.   * variables. All operations should be accessed only through
  30.   * methods provided by the class.
  31.   */
  32. public static void main(String[] args) {
  33. File targetFile = new File("t.txt");
  34. try {
  35. FileProcessor fileProcessor = new FileProcessor(targetFile);
  36. fileProcessor.printFile();
  37. } catch (FileNotFoundException fnf) {
  38. System.out.println("Target file: " + targetFile + " not found.");
  39. System.exit(1);
  40. } catch (IOException ex) {
  41. System.out.println("An IO error occurred processing this file");
  42. ex.printStackTrace();
  43. System.exit(1);
  44. }
  45. }
  46.  
  47. }
Reply With Quote