2017-11-16 11 views
0

lineNumberを変数に解決できないというエラーが表示され続けますか?私はこれを正確に修正する方法が本当にわからない。これに役立つ特定のファイルをjavaにインポートしていませんか?仕上げファイルクラス

また、空白文字と空白文字の数をどのように数えますか?

また、ユニークな単語を数える方法が必要ですが、ユニークな単語は本当にわかりません。

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

正確なエラーメッセージを投稿し、質問を1つの問題に限定してください。 – ADyson

答えて

0

変数lineNumberはデータ型で宣言する必要があります。

int lineNumber = 2; // uはJavaですべての変数のデータ型を設定することが重要であるとして、そのデータ型を設定することにより、int型LINENUMBER = 2

変更にちょうどLINENUMBERからmainメソッドの最初の行をしたいと。

1

あなたは、少なくとも2つの変更を行う必要が実行されているコードを取得するために:

は交換してください:

int lineNumber=2; // as u want 

lineNumber=2; // as u want 

をまた、あなたはあなたのmain方法を変更する必要があります例外をキャッチするために何も上にないので、mainメソッド宣言で例外をスローすることはできません。例外を処理する必要がありますそれは内部のS:

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

私はグローバルExceptionキャッチを使用しました、あなたはseparatedlyそれらを処理するためには、いくつかのキャッチでexpetionを分離することができます。それは私に明白なFileNotFoundExceptionを教える例外を与えますが、あなたのコードは今実行されています。

+0

スペースでスペースを入れずに文字を数えるには、何を追加する必要がありますか? –

+0

私は基本的に – assembler

+0

にファイルのテキスト間のスペースを数えさせているかどうか分かりません。 –

関連する問題