tags:

views:

755

answers:

4

This example demonstrates using Scanner to read a file line by line (it does not perform a write operation) I don't know why I get an error when I try to compile. Could somebody explain the reason to me?. I'm using jcreatorLE and JDK 1.6 to run my program:

import java.io.*;
import java.util.Scanner;

public final class File_read {

  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;
  }
}

This is the result from running it:

--------------------Configuration: <Default>--------------------
C:\Users\administrador\Documents\File_read.java:15: invalid method declaration; return type required
  public ReadWithScanner(String aFileName){
         ^
1 error

Process completed.
A: 

The name of the class is File_read, so the constructor name should be File_read but you gave the name as ReadWithScanner that is why its complaining. Compiler thinking its a method name so expecting a return type.

Bhushan
+3  A: 

Your "ReadWithScanner" constructor needs to match the name of the class ("File_read")

public File_read(String aFileName){
    fFile = new File(aFileName);  
}
Andy White
+1  A: 

Your class is named File_read and your constructor is named ReadWithScanner. The warning is that your constructor needs to be named the same as the class.

Clint
+6  A: 

When you lifted that code from here :-), you renamed the class but not the constructor. Only constructors are allowed to not have return types.

I suggest you either rename the class back or rename the constructor.

I hope this isn't homework. As it stands, your educator would have an easy time proving plagiarism. You'll need to at least change the variable names as well as class names, you might want to also reformat it a bit including changing the order of methods in the class.

That's if it's homework. Which it's not, right? :-)

paxdiablo
thanks for the help, it's not my homework, my school does not teach programming .... I was reading a tutorial but I'm bad for programs = (.. I'm new to the world of programming ... I hope not bother ... I have 13 years =P
Melkhiah66
Don't give up, mate, knowledge accumulation is an accelerating process. It may seem slow now, but the speed will pick up.
paxdiablo