Results 1 to 9 of 9
Thread: please help me
- 06-13-2010, 07:10 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
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:
please help me how can i use of this class in main of blogfa.classJava 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; } }
Moderator Edit: Code tags addedLast edited by Fubarable; 06-13-2010 at 07:53 PM. Reason: Moderator Edit: Code tags added
- 06-13-2010, 08:04 PM #2
Please copy and paste your errors here.many errors that are produced
- 06-14-2010, 03:17 AM #3
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
show us the error trace here
- 06-14-2010, 04:31 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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,
which is an invalid method declaration. You must correct it first of all. Hope you can find what the error is.Java Code:public ReadWithScanner(String aFileName) { fFile = new File(aFileName); }
- 06-14-2010, 04:33 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And also the way you call/invoke methods are completely wrong.
Java Code:ReadWithScanner parser = new ReadWithScanner("C:\\Temp\\test.txt");
- 06-14-2010, 04:35 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-14-2010, 05:13 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 06-15-2010, 04:12 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-15-2010, 04:16 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks