Results 1 to 9 of 9

Thread: please help me

  1. #1
    ansar313 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default please help me

    in the name of god
    hi

    i want use processLineByLine() function in main function of blogfa class But many errors that are produced prevent me.
    the ReadWithScanner.java class code:

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public final class ReadWithScanner {
    
      public static void main(String... aArgs) throws FileNotFoundException {
        ReadWithScanner parser = new ReadWithScanner("C:\\Temp\\test.txt");
        parser.processLineByLine();
        log("Done.");
      }
      
      /**
      * @param aFileName full name of an existing, readable file.
      */
      public ReadWithScanner(String aFileName){
        fFile = new File(aFileName);  
      }
      
      /** Template method that calls {@link #processLine(String)}.  */
      public final void processLineByLine() throws FileNotFoundException {
        Scanner scanner = new Scanner(fFile);
        try {
          //first use a Scanner to get each line
          while ( scanner.hasNextLine() ){
            processLine( scanner.nextLine() );
          }
        }
        finally {
          //ensure the underlying stream is always closed
          scanner.close();
        }
      }
      
      /** 
      * Overridable method for processing lines in different ways.
      *  
      * <P>This simple default implementation expects simple name-value pairs, separated by an 
      * '=' sign. Examples of valid input : 
      * <tt>height = 167cm</tt>
      * <tt>mass =  65kg</tt>
      * <tt>disposition =  "grumpy"</tt>
      * <tt>this is the name = this is the value</tt>
      */
      protected void processLine(String aLine){
        //use a second Scanner to parse the content of each line 
        Scanner scanner = new Scanner(aLine);
        scanner.useDelimiter("=");
        if ( scanner.hasNext() ){
          String name = scanner.next();
          String value = scanner.next();
          log("Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim()) );
        }
        else {
          log("Empty or invalid line. Unable to process.");
        }
        //(no need for finally here, since String is source)
        scanner.close();
      }
      
      // PRIVATE //
      private final File fFile;
      
      private static void log(Object aObject){
        System.out.println(String.valueOf(aObject));
      }
      
      private String quote(String aText){
        String QUOTE = "'";
        return QUOTE + aText + QUOTE;
      }
    }
    please help me how can i use of this class in main of blogfa.class

    Moderator Edit: Code tags added
    Last edited by Fubarable; 06-13-2010 at 07:53 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    many errors that are produced
    Please copy and paste your errors here.

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    show us the error trace here

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, it's much better to post your complete error message here to see. Most probably no one wants to run your code and find what are the errors. :)

    Just looking at your code I found this,

    Java Code:
    public ReadWithScanner(String aFileName) {
            fFile = new File(aFileName);
        }
    which is an invalid method declaration. You must correct it first of all. Hope you can find what the error is.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also the way you call/invoke methods are completely wrong.

    Java Code:
    ReadWithScanner parser = new ReadWithScanner("C:\\Temp\\test.txt");

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Looking at your code, my suggestion is start to learn Java from the very basis with very simple example. This is not a good example to learn those things.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Eranga View Post
    Just looking at your code I found this,

    Java Code:
    public ReadWithScanner(String aFileName) {
            fFile = new File(aFileName);
        }
    which is an invalid method declaration. You must correct it first of all. Hope you can find what the error is.
    That's a constructor, not a method, and fFile is an instance variable (awkwardly declared in the middle of the file, rather than at the very top or very bottom), so that part is OK.

    OP, you want to use a method from one class within another class. Since it's not a static method, the best way to do this is to create a ReadWithScanner object and call its processLineByLine() method. You don't show us your blogfa (both rather poor class names, btw) code, so we can't guess what's wrong with it.

    -Gary-

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    awkwardly declared in the middle of the file, rather than at the very top or very bottom
    Oh yeah. Since it's not formatted the way I do, I didn't recognized that. Sorry about that, dry-run not always success. ;)

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post
    And also the way you call/invoke methods are completely wrong.

    Java Code:
    ReadWithScanner parser = new ReadWithScanner("C:\\Temp\\test.txt");
    My apology on this. I'm confused with the class name. This is correct actually, since accessing static members.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •