java文本文件读取程序,只读取txt文件的第一行

时间:2014-01-13 00:24:13

标签: java

我写了一个程序,它读取字母a,e,s和t以及空格出现在txt文件中的次数,它当前有效,但只读取txt文件的第一行。如何让我的程序读取txt文件中的所有行,然后输出这些字母的使用次数?感谢您的时间和帮助。

    import java.util.Scanner;
    import java.io.FileNotFoundException;

    public class Count
    {
      public static void main (String[] args) throws  FileNotFoundException {

      String phrase;  // a string of characters
      int countBlank;   // the number of blanks (spaces) in the phrase 
      int length;       // the length of the phrase
      char ch;          // an individual character in the string
      int countA;
      int countE;
      int countS;
      int countT;

      java.io.File file = new java.io.File("counting.txt");
      Scanner inFile = new Scanner (file);

     Scanner scan = new Scanner(System.in);

     phrase = inFile.nextLine();
     length = phrase.length();

          // Initialize counts

      while (true)
      { 
      if (phrase.equalsIgnoreCase("quit"))

          break;

      else
      {

      countBlank = 0;
      countA = 0;
      countE = 0;
      countS = 0;
      countT = 0;

      for ( int i = 0; i < length; i++ )   
       { 
       if ( phrase.charAt( i ) == ' ' )

        countBlank++;
        ch = phrase.charAt(i);

           switch (ch)
            {
             case 'a':
             case 'A':  countA++;
                     break;
         case 'e':
         case 'E':  countE++;
             break;
         case 's':
         case 'S':  countS++;
                break;
         case 't':
         case 'T':  countT++;
            break;
          }

     }
            System.out.println ();
            System.out.println ("Number of blank spaces: " + countBlank);
            System.out.println ();

        System.out.println ("Number of A's: " + countA);
        System.out.println ();
        System.out.println ("Number of E's: " + countE);
        System.out.println ();
        System.out.println ("Number of S's: " + countS);
        System.out.println ();
        System.out.println ("Number of T's: " + countT);
        break;

      }     
     }


     } 
    }

4 个答案:

答案 0 :(得分:1)

您可以尝试使用while-loop循环覆盖该文件。

phrase = inFile.nextLine();替换为:

while(inFile.hasNextLine()) // While there are lines in the file
    phrase += inFile.nextLine(); // Add line to 'phrase'

不要忘记用空字符串初始化String phrase

String phrase = "";

修改:您的最终代码应如下所示,并在此答案的末尾列出了一些更改。

public static void main(String[] args) throws FileNotFoundException
{

    String phrase = ""; // a string of characters
    int countBlank; // the number of blanks (spaces) in the phrase 
    int length; // the length of the phrase
    char ch; // an individual character in the string
    int countA;
    int countE;
    int countS;
    int countT;

    java.io.File file = new java.io.File("sample.txt");
    Scanner inFile = new Scanner(file);

    while (inFile.hasNextLine())
        phrase += inFile.nextLine(); // Add line to 'phrase'
    length = phrase.length();

    // Initialize counts

    while (true) {

        countBlank = 0;
        countA = 0;
        countE = 0;
        countS = 0;
        countT = 0;

        for (int i = 0; i < length; i++) {
            ch = phrase.charAt(i);

            switch (ch)
            {
            case 'a':
            case 'A':
                countA++;
                break;
            case 'e':
            case 'E':
                countE++;
                break;
            case 's':
            case 'S':
                countS++;
                break;
            case 't':
            case 'T':
                countT++;
                break;
            case ' ':
                countBlank++;
                break;
            default:
                break;
            }

        }
        System.out.println();
        System.out.println("Number of blank spaces: " + countBlank);
        System.out.println();

        System.out.println("Number of A's: " + countA);
        System.out.println();
        System.out.println("Number of E's: " + countE);
        System.out.println();
        System.out.println("Number of S's: " + countS);
        System.out.println();
        System.out.println("Number of T's: " + countT);
        break;

    }

}

<强>的变化:

  • 删除Scanner scan = new Scanner(System.in);,因为您不使用它。
  • 添加了我上面建议的代码
  • 已删除if (phrase.equalsIgnoreCase("quit")) break;
  • switch-case
  • 时添加了ch == ' '
  • 添加default-case,因为它被认为是良好的编程习惯。

答案 1 :(得分:0)

当然,您应该使用while来包裹inFile.nextLine()来生成线条。但是,因为你只使用它一次只产生第一行。

尝试使用以下代码包装代码:

while ((phrase = inFile.nextLine()) != null) {
    // Here code
}

答案 2 :(得分:0)

您需要阅读字符串“短语”的下一行。 添加

  

phrase = inFile.nextLine();

到while循环结束。

答案 3 :(得分:0)

The Line

phrase = inFile.nextLine();

不在你的while循环中。因此它只执行一次。将它添加到while循环中