1

この例では、Scanner を使用してファイルを 1 行ずつ読み取る方法を示します (書き込み操作は実行しません)。コンパイルしようとするとエラーが発生する理由がわかりません。誰かが私に理由を説明してもらえますか?. プログラムを実行するために jcreatorLE と JDK 1.6 を使用しています。

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

これを実行した結果は次のとおりです。

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

Process completed.
4

4 に答える 4

6

ここからそのコードを持ち上げたとき:-)、クラスの名前を変更しましたが、コンストラクターの名前は変更しませんでした。戻り値の型を持たないことが許されるのはコンストラクターだけです。

クラスの名前を元に戻すか、コンストラクターの名前を変更することをお勧めします。

これが宿題でないことを願っています。現状では、教育者は盗作を簡単に証明できます。少なくとも変数名とクラス名を変更する必要があります。クラス内のメソッドの順序を変更するなど、少し再フォーマットすることもできます。

宿題ならそうです。そうじゃないでしょ?:-)

于 2009-04-20T04:07:29.810 に答える
3

「ReadWithScanner」コンストラクターは、クラスの名前 (「File_read」) と一致する必要があります。

public File_read(String aFileName){
    fFile = new File(aFileName);  
}
于 2009-04-20T04:06:21.700 に答える
1

クラスには名前が付けられFile_read、コンストラクタには名前が付けられReadWithScannerます。警告は、コンストラクターにクラスと同じ名前を付ける必要があることです。

于 2009-04-20T04:07:13.400 に答える
0

クラスの名前は File_read であるため、コンストラクター名は File_read にする必要がありますが、名前を ReadWithScanner として指定したため、不平を言っています。コンパイラはそれをメソッド名と考えているため、戻り値の型を期待しています。

于 2009-04-20T04:05:44.900 に答える