2017-03-03 116 views
0

我目前正在为我的介绍编程类做一个任务,我需要能够将文件的内容分配给变量。该文件包含一个文本段落。我需要能够最终统计文本文件中的所有字符和单词。将文件内容分配给变量

我到目前为止有:

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

public class CountWords { 
    public static void main(String[] args) throws IOException { 

    File inFile = new File("CountWordsTestFile.txt"); 
    Scanner input = new Scanner(inFile); 

    String text = input.nextLine(); 

    int words = 0, cha = text.length(), character = 0; 


     System.out.println(text); 

    System.out.println("The file contains"); 
    System.out.println(words + " words"); 
    System.out.println((cha + 1) + " characters"); 
     input.close(); 
    } 
} 
+0

提示:使用StringBuilder text = new StringBuilder(),然后遍历输入和text.append(input.nextLine())以将所有文本添加到Stringbuilder。 – mba12

+2

你的问题是什么? – vanje

+2

@vanje他需要我们做他的功课:) .. – Maverick

回答

0
package test; 
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 


public class ReadFile { 

    public static void readWords(String filePath) { 
     int words = 0; 
     int characters = 0; 

     BufferedReader br = null; 
     FileReader fr = null; 
     try { 
      fr = new FileReader(filePath); 
      br = new BufferedReader(fr); 
      String line; 
      while ((line = br.readLine()) != null) { 
       if (line.trim().replaceAll(" +", " ").length() == 0) continue; 
       characters += line.length(); 

       line = replaceAllSpecialCharacters(line); 
       words += line.split("\\s").length; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (br != null) 
        br.close(); 

       if (fr != null) 
        fr.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     System.out.println("Words : " + words); 
     System.out.println("Characters : " + characters); 


    } 

    public static String replaceAllSpecialCharacters(String line) { 

     return line.replaceAll(" +", " ").replaceAll("[^\\p{L}\\p{Z}]", " ").trim(); 

    } 

    public static void main(String[] a) { 
     readWords("CountWordsTestFile.txt"); 
    } 

} 

编辑: 简单的代码为您的要求

package test; 

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


public class ReadFile { 

    public static void readWords(String filePath) { 
     int words=0,characters=0; 

     Scanner in= null; 
     try { 
      in = new Scanner(new File(filePath)); 


     while (in.hasNextLine()){ 
      //Here you have the value line by line 

      String line =in.nextLine(); 
      //if line is empty or has only spaces continue to the another line 
      if(line==null||line.trim().length()==0)continue; 

      //add the length for each line to compute characters 

      characters+=line.length(); 



      //replace multiple spaces with one space 
      line=line.replaceAll(" +"," "); 

      //trim the line to remove spaces 

      line=line.trim(); 

      //split the line with space to get array of words 

      String []wordsArray=line.split("\\s"); 




      //the length of this array is the # of words in this line 

      words+=wordsArray.length; 

     } 


     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     System.out.println("Words : " + words); 
     System.out.println("Characters : " + characters); 
    } 

    public static void main(String[] a) { 
     readWords("CountWordsTestFile.txt"); 
    } 



} 
+0

欣赏这一点,但我在一个BEGINNER编程类。你会知道一种方式来完成相同的使用可能是一个数组或某种循环(while,for等)。 – thedon

+0

是的,肯定会看到答案 – 2017-03-03 21:43:34

+0

哦应该在之前提到过,扫描仪声明中的所有内容在我的最终计划中都是强制性的。所以,如果不改变代码,并可能使用扫描仪对象,那么...对不起,大声笑 – thedon

0

这里被简化Java程序的版本在一个文件来计算的字符和单词。我在这里假设文件是​​一个文本文件,文件中的单词只由一个或多个空格分隔,而不是由任何分隔符分隔。

public class CountWords { 
    public static void main(String[] args) { 
     //Initializing counter for words and characters in the file 
     int words = 0, chars = 0; 
     File inFile = new File("CountWordsTestFile.txt"); 
     try { 
      Scanner scanner = new Scanner(inFile); 
      while(scanner.hasNextLine()){ 
       //Read the line and assign it to a string 
       String str = scanner.nextLine(); 
       //Increase the counter for no of characters 
       chars = chars + str.length(); 
       //Split the line by spaces and store in an array 
       String[] strArr = str.split("[ ]+"); 
       //Increase the counter for no of words 
       words = words + strArr.length; 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     System.out.println("The file contains"); 
     System.out.println(words + " words"); 
     System.out.println(chars + " characters"); 
    } 
} 

希望它符合您的要求。

+0

如果你写完整的代码,你可以做得对:使用资源尝试,不要吞下异常,并使用\ s + in正则表达式。如果你真的希望OP从你的答案中学到一些东西,请解释一下,至少要指出你的答案和OP的尝试之间的差异。 – Robert