2015-12-14 23 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();

相关问题