How to Remove Main()


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to Remove Main()

  1. #1
    Join Date
    Dec 2004
    Posts
    1

    How to Remove Main()

    I've written a stand alone program that performs simple text i/o. I would like to remove the main() method.

    Are there any java experts in here that would show the code to split the attached program file into a ReadFileIn and a ReadFileInTestDrive?

    I'm new to objects and I'm having a difficult time understanding how to move the main method from ReadFileIn.java to a new program ReadFileInTestDrive.java.
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    No big deal really. Read my comments.

    Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.File;
    import java.util.regex.*;
    
    /**
     * >> Put this class in s file called  ReadFileInDriver.java
     *
     */
    public class ReadFileInDriver {
      public static void main(String[] args) {
        if (args.length != 1)
          System.err.println("Usage: java ReadFileIn <source>");
        else {
          // you could have passed the filepath to the constructor, so when you 
          // want to parse several files, then the parse result could be kept by 
          // different ReadFileIn objects, each identifieable by the file they have
          // parsed.
          ReadFileIn rIn=new ReadFileIn();
          try {
            rIn.readit( args[0] );
          } catch(IOException e) {
            e.printStackTrace();
          }
        }
      }
    
    }
    /**
    * A program that reads an ascii, comma-delimited file
     * >> put this class in a file called ReadFileIn.java,
     * >> AND declare it as 'public class ReadFileIn' -> When classes share
     * >> the same file then only one can be 'public'
    **/
    
    class ReadFileIn {
      public ReadFileIn() {} // default constructor, does basically nothing
      /**
       * readit is the static method that performs the file read.
       **/
    
      public static void readit(String from_name) throws IOException {
        File from_file = new File(from_name);
    
        // does source file exist and is it readable
        if (!from_file.exists())
          System.out.println("no source file: " + from_name);
        if (!from_file.isFile())
          System.out.println("unable to read directory: " + from_name);
        if (!from_file.canRead())
          System.out.println("source file is unreadable: " + from_name);
    
          // read all lines from the file
    
        //first create a FileReader Object
        // then create a BufferedReader so we can use the readLine() method
        // could have done it with one line : BufferedReader in = new BufferedReader(new FileReader(from_name));
    
        try {
    
          FileReader filer = new FileReader(from_name);
          BufferedReader reader = new BufferedReader(filer);
    
          int count = 0;
          int blanks = 0;
    
          String line = null;
    
          // read a line of text, assign it to line variable, while not null
          // loop
    
          while ( (line = reader.readLine()) != null) {
    
            System.out.println(line);
    
            count++;
    
            if (line.equals("")) {
              blanks++;
            }
    
            String[] fieldArray = line.split(",");
    
            for (int i = 0; i < fieldArray.length; ++i) {
    
              System.out.println(fieldArray[i]);
    
            }
    
          }
    
          reader.close();
    
          System.out.println("lines: " + count + " blank lines: " + blanks);
        }
        catch (IOException except) {
          except.printStackTrace();
        }
      }
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center