2013-09-01 3 views
0

저는 학교 과제를 위해 일하고 있습니다. 이 프로그램은 사용자에게 파일 이름을 묻고 해당 파일의 문자, 단어 및 줄 수를 계산합니다. 그런 다음 다음 파일의 이름을 묻습니다. 사용자가 존재하지 않는 파일을 입력하면 프로그램은 처리 된 모든 파일에 문자, 단어 및 행의 총 수를 인쇄하고 존재합니다.변수가 로컬에서 읽을 수 없습니다. 이유를 알아낼 수 없습니다

그래서 프로그램을 작성했지만 몇 가지 오류가 나타납니다. 여기에 나는

private FileCounter counter; 

private boolean done; 

오류는 말한다 줄에 오류가 발생하고, 카운터 프로그램입니다 : FieldCounter.done는 로컬 읽어 본 적이있다. 이전 줄에서도 마찬가지입니다. 나는이 경고를받는 이유를 알 수 없다.

프로그램의 나머지 :

import java.util.Scanner; 
    import java.io.FileReader; 
    import java.io.FileNotFoundException; 
    /** 
    * A class to count the number of characters, words, and lines in files. 
    */ 

    public class FileCounter 
    { 
     /** 
      Constructs a FileCounter object. 
     */ 
     public FileCounter() 
     { 
     words = 0; 
     lines = 0; 
     chars = 0; 
     input = ""; 

    } 

     /** 
      Processes an input source and adds its character, word, and line 
      counts to this counter. 
      @param in the scanner to process 
     */ 
     public void read(Scanner in) throws FileNotFoundException 
     { 

      boolean done = false; 
      while (!done) 
      { 
       while(in.hasNextLine()) 
       { 
        lines++; 
        words++; 
        int j = 0; 
        file = in.nextLine(); 
        input = input + file; 
        for(int i = 1; i < file.length(); i++) 
        { 
        if(file.substring(j, i).equals(" ")) 
        { 
        words++; 
        } 


        j++; 

       } 
        } 
        char[] array = input.toCharArray(); 
        int num = array.length; 
        chars += num; 
        if(in.hasNextLine() == false) 
        done = true; 
       } 



     } 
     /** 
      Gets the number of words in this counter. 
      @return the number of words 
     */ 
     public int getWordCount() 
     { 
     return words; 
     } 

     /** 
      Gets the number of lines in this counter. 
      @return the number of lines 
     */ 
     public int getLineCount() 
     {  
      return lines; 
     }  

     /** 
      Gets the number of characters in this counter. 
      @return the number of characters 
     */ 
     public int getCharacterCount() 
     { 

      return chars; 
     } 

     private String input; 
     private int words; 
     private FileCounter counter; 
     private int lines; 
     private boolean done; 
     private int chars; 
     private String file; 

    } 


import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.util.Scanner; 

/** 
    This class prints a report on the contents of a number of files. 
*/ 
public class FileAnalyzer 
{ 
    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     FileCounter counter = new FileCounter(); 
     boolean more = true; 
     while (more) 
     { 
     System.out.print("Please enter the next filename, or <Enter> to quit: "); 
     String filename = in.nextLine(); 
     if (filename.length() > 0) 
     { 
      try 
      { 
       FileReader fileRead = new FileReader(filename); 
       Scanner fileInput = new Scanner(fileRead); 
       counter.read(fileInput); 
      } 
      catch (FileNotFoundException fnfe) 
      { 
       System.out.println("File " + filename + " was not found: " + fnfe); 
      } 
     } 
     else 
     { 
      more = false; 
     } 
     } 
     System.out.println("Characters: " + counter.getCharacterCount()); 
     System.out.println("Words: " + counter.getWordCount()); 
     System.out.println("Lines: " + counter.getLineCount()); 
    } 
} 
+0

@ PM77-1 덕분에 오류를 표시하기 위해 내 게시물을 편집했습니다. 나는 당신이 논평하는 동안 편집 중이었습니다. 너희들은 너무 빠르다 :-) –

답변

1

할당되지 않은 전용 멤버 변수 done이 있습니다. 아마도 로컬 변수 doneread 메서드에 선언하고이를 사용하기 때문입니다. FileCounter.done과 완전히 다른 변수이므로 FileCounter.done은 절대로 사용되지 않습니다.

변경 :

boolean done = false; 

단순히 : 당신은 더 이상 별도의 지역 변수를 만드는 대신 클래스의 멤버 변수를 사용하는 것입니다

done = false; 

.

또는 멤버 변수를 제거 private boolean done;을 제거 할 수 있지만 - 당신이 할 경우 - done이 지역 변수임을 기억하고 이 원하는 것을 아마 인 (read에 여러 통화를 통해 그 가치을 유지하지 않습니다 케이스, 그러나 구별을 이해하는 것이 중요하다).

1

변수는 읽어 본 적이대로 본질적으로 쓸모가 당신이 수단을 받고 경고. 그 라인들과 그것들에 대한 모든 언급을 삭제하십시오. 이론적으로 프로그램은 여전히 ​​실행될 것이고 변경은 일어나지 않아야합니다.

private boolean done; 이것은 결코 사용되지 않습니다. private FileCounter counter; 이렇게됩니다.

1

오류가 아니므로 경고입니다. 프로그램은 정상적으로 컴파일되지만 이상한 것을 발견했습니다. 이 경우

당신은 private 부울 속성을 정의하고 있지만, 당신은 아무것도를 위해 그것을 사용하지 않습니다 (당신이 당신의 read 방법에서 사용하는 done이 방법에 로컬로 정의 된 것을 알, 그래서 다른 변수입니다).

private boolean done 줄만 삭제하면됩니다.

counter에도 동일하게 적용됩니다.

관련 문제