2017-11-16 16 views
0

lineNumber를 변수로 해석 할 수 없다는 오류가 계속 나타 납니까? 이 문제를 정확하게 해결하는 방법을 잘 모르겠습니다. 내가 이걸로 도움이되는 특정 파일을 자바로 가져 오지 않았습니까?마무리 파일 클래스

또한 공백없이 공백없이 문자의 수를 어떻게 계산합니까?

또한 고유 한 단어를 계산하는 방법이 필요하지만 고유 한 단어가 무엇인지 잘 모르겠습니다.

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Scanner; 
import java.util.StringTokenizer; 
import java.util.ArrayList; 
import java.util.List; 

public class LineWordChar { 
public void main(String[] args) throws IOException { 
    // Convert our text file to string 
String text = new Scanner(new File("way to your file"), "UTF-8").useDelimiter("\\A").next(); 
BufferedReader bf=new BufferedReader(new FileReader("way to your file")); 
String lines=""; 
int linesi=0; 
int words=0; 
int chars=0; 
String s=""; 

// while next lines are present in file int linesi will add 1 
    while ((lines=bf.readLine())!=null){ 
    linesi++;} 
// Tokenizer separate our big string "Text" to little string and count them 
StringTokenizer st=new StringTokenizer(text); 
while (st.hasMoreTokens()){ 
    s = st.nextToken(); 
     words++; 
// We take every word during separation and count number of char in this words  
     for (int i = 0; i < s.length(); i++) { 
      chars++;} 
    } 
System.out.println("Number of lines: "+linesi); 
System.out.println("Number of words: "+words); 
System.out.print("Number of chars: "+chars); 
} 
} 
abstract class WordCount { 

/** 
* @return HashMap a map containing the Character count, Word count and 
*   Sentence count 
* @throws FileNotFoundException 
* 
*/ 
public static void main() throws FileNotFoundException { 
    lineNumber=2; // as u want 
    File f = null; 
    ArrayList<Integer> list=new ArrayList<Integer>(); 

    f = new File("file_stats.txt"); 
    Scanner sc = new Scanner(f); 
    int totalLines=0; 
    int totalWords=0; 
    int totalChars=0; 
    int totalSentences=0; 
    while(sc.hasNextLine()) 
    { 
     totalLines++; 
     if(totalLines==lineNumber){ 
      String line = sc.nextLine(); 
      totalChars += line.length(); 
      totalWords += new StringTokenizer(line, " ,").countTokens(); //line.split("\\s").length; 
      totalSentences += line.split("\\.").length; 
      break; 
     } 
     sc.nextLine(); 

    } 

    list.add(totalChars); 
    list.add(totalWords); 
    list.add(totalSentences); 
    System.out.println(lineNumber+";"+totalWords+";"+totalChars+";"+totalSentences); 

} 
} 
+1

정확한 오류 메시지를 게시하고 질문을 한 가지 문제로 제한하십시오. – ADyson

답변

0

lineNumber 변수는 데이터 유형으로 선언해야합니다.

int lineNumber = 2; // U 자바에서 각 변수의 데이터 타입을 설정하는 것이 중요 같은 데이터 타입을 설정하여 INT LINENUMBER = 2

변화를 단지 LINENUMBER에서 주요 방법에서 첫 번째 행하고자한다.

1

당신은 적어도 두 가지 사항을 변경해야 할 실행 코드를 얻기 위해 : 교체

을 :

lineNumber=2; // as u want 

또한

int lineNumber=2; // as u want 

와 함께, 당신은 당신의 main 방법을 수정해야 예외를 잡으려고 위의 아무것도 없으므로 main 메서드 선언에 예외를 throw 할 수 없습니다. 예외를 처리해야합니다. 그 안에 S : 나는 글로벌 Exception 캐치를 사용했습니다

public static void main(String[] args) { 
     // Convert our text file to string 
     try { 
      String text = new Scanner(new File("way to your file"), "UTF-8").useDelimiter("\\A").next(); 
      BufferedReader bf = new BufferedReader(new FileReader("way to your file")); 
      String lines = ""; 
      int linesi = 0; 
      int words = 0; 
      int chars = 0; 
      String s = ""; 

      // while next lines are present in file int linesi will add 1 
      while ((lines = bf.readLine()) != null) { 
       linesi++; 
      } 
      // Tokenizer separate our big string "Text" to little string and count them 
      StringTokenizer st = new StringTokenizer(text); 
      while (st.hasMoreTokens()) { 
       s = st.nextToken(); 
       words++; 
       // We take every word during separation and count number of char in this words 
       for (int i = 0; i < s.length(); i++) { 
        chars++; 
       } 
      } 
      System.out.println("Number of lines: " + linesi); 
      System.out.println("Number of words: " + words); 
      System.out.print("Number of chars: " + chars); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

, 당신은 separatedly을 처리하기 위해 여러 어획량에 expetion을 분리 할 수 ​​있습니다. 그것은 저에게 명백한 FileNotFoundException을 말하는 예외를 제공합니다. 게다가 코드가 지금 실행됩니다.

+0

공백없이 공백없이 숯을 계산하려면 무엇을 추가해야합니까? –

+0

난 당신이 파일의 텍스트 사이에 공간을 계산하기 위해 기본적으로 – assembler

+0

을 요구하고있다 모르겠어요. –