2015-12-14 26 views
-1

我正在編寫一個應該讀取簡單文本文件的程序,並輸出該.txt文件中所有字母的列表,並使用最常用的字母最不常用的字母。可以在.txt文件中以字母讀取的代碼

我完成了一個工作的Java程序的編碼工作,它要求輸入文件名並輸出文件中的文本。但我不確定如何去輸出這些字母的列表。我不確定的是讀者類中的哪些方法(如果有的話)可以使用讀取.txt文件中的每個字母。任何幫助,將不勝感激!

這是當前的代碼:

// Here I import the Bufered Reader and file reader Libraries 
// The Buffered Reader library is similar to Scanner Library and 
// is used here to read from a text file. File reader will allow 
// the program to access windows file system, get the text file 
// and allow the Buufered Reader to read it in. 
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.util.Scanner; 

public class TextFileReaderApp 
{ 
    // I added "throws exception" in case there is an an error in the 
    // main method, throw an exception, so it can prevent further 
    // errors from occuring if java doesnt know the main methods going 
    // to throw an error. 
    public static void main(String[] args) throws Exception 
    { 
     // below I diplay a welcome messgae to the user 
     System.out.println(); 
     System.out.println("Welcome to the Text File Reader application!"); 
     System.out.println(); 

     // Below I create an instance of the Scanner class to get 
     // input from the user. 
     Scanner userInput = new Scanner(System.in); 
     String selection = "y"; //this is the string variable that's used in 
           //the while loop to continue the program. 

     // Below I created a while loop that continues the program if the user 
     // keeps selecting y as their selecion 
     while (selection.equalsIgnoreCase("y")) 
     { 
      // this line of code is supposed to ask the user for text file name under 
      // the C:/ directory and must not be hidden in any foler. 
      System.out.print("Please enter the name of the .txt file: C/"); 
      FileReader file = new FileReader("C:/" + userInput.next()); 

      // file object is used as a parameter in buffered reader. 
      BufferedReader textReader = new BufferedReader(file); 

      // below I create and initialize an object of type string called text that will 
      // store whats inside of the text file. 
      String text = ""; 

      // I use the readLine statement to read line after line of the text. 
      // Once it has read everything it will return null. 
      String lineText = textReader.readLine(); 

      // code below is a test for me to see if the code above works and is able to read 
      // the text inside the file and output it. 
      while(lineText != null) 
      { 
       // this reads the text line for line and ads it to the text variable for output. 
       text = text + lineText + "\n"; 
       lineText = textReader.readLine(); 
      } 
      System.out.println(text); 
     } 
     // These 3 code lines ask the user if he/she would like to continue with the program. 
     System.out.println(); 
     System.out.print("Continue using the Text File Reader? (y/n): "); 
     choice = user_input.next(); 
     System.out.println(); 
    } 
} 
+2

您將要閱讀java中Reader類的javadoc。 – jtahlborn

+1

這是一系列聲明,要求我們爲您編寫代碼,而不是真正的問題。 – redFIVE

+0

不,我不指望任何人爲我編寫代碼,但我只是想知道讀者類中可以讀取文本中單詞的字母的方法。我可以處理除此之外的所有事情,但我只是想澄清。 – KillaFrostByte

回答

0

如果你需要統計的字母/字符,你可以做到一樣好上線/詞等沒有必要在這裏涉及到閱讀器。

for (char c : someString.toCharArray()) { 
    // handle the character 
    } 

一旦你從你的文件中有任何字符串應該工作。

0

首先,您可能希望使用StringBuilder而不是String文本,因爲它具有更好的性能。 「text = text + lineText」將在每次執行時創建另一個String對象,在這種情況下StringBuilder的效果更好)。

實現所需內容的一種方法是讀取textLine的字符並將所有字母使用switchcase塊,並在發生時將它們添加到包含整數的數組中。例如:

int[] array = new int[26]; 
switch(character){ 
case "a": 
    array[0] += 1; 
    break; 
case "b": 
    array[1] += 1; 
    break; 
//.... 
} 

等等... 在你使用一個簡單的for循環和打印數組的值結束。現在你會看到你輸入了多少次字符。

+0

感謝您的輸入!所以從我的理解,我將不得不爲每個字母編寫代碼,將數組元素的數量增加一個?我可以創建一個包含26個元素的數組,並且每次讀入一個字符時都會將某個元素加1。有一個問題,我可以使用if/else if語句而不是switchcase塊嗎?我對使用switchcase塊不太熟悉,對於java來說,我還是相當新的。 – KillaFrostByte

+0

是的,這將是我的建議。 當然,你也可以用if/else塊來完成它,但是使用開關/外殼看起來更清潔並且更易於閱讀。 您可能需要查看此頁面: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – R3KHYT

0

這會從textReader中讀取所有字符,直到到達EOF或發生異常。

try { for(int i = textReader.read(); i != -1 /* EOF */; i = textReader.read()) { char c = (char) i; // do whatever you want with your char here } } catch(IOException) textReader.close();

相關問題