2014-01-13 302 views
0

我寫了一個程序,讀取字母a,e,s和t以及空格在txt文件中出現的次數,它當前工作正常,但只能讀取txt文件的第一行。如何讓我的程序讀取txt文件中的所有行,然後輸出這些字母被使用的次數?感謝您的時間和幫助。java 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; 

     }  
    } 


    } 
    } 

回答

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,因爲它被認爲是良好的編程習慣。
+0

我對程序進行了這些更改,它仍然返回相同的內容。 – user3188576

+0

它適合我。你的文件是怎樣的? – Christian

+0

txt文件或java文件? – user3188576

0

當然,如果要在使用while包裹inFile.nextLine()生產線你。但是,既然你只使用它,它只會讓你成爲第一行。

嘗試包裝你這個代碼:

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

我做到了,現在程序爲所有字母返回0。爲什麼會發生? – user3188576

0

您需要讀取字符串「phrase」的下一行。 Add

phrase = inFile.nextLine();

到while循環的結尾。

0

phrase = inFile.nextLine(); 

是你的while循環。因此它只執行一次。將它添加到你的while循環中