我写了一个程序,读取txt文件中字母a、e、s和t以及空格出现的次数,目前它可以工作,但只读取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;
}
}
}
}
最佳答案
您可以尝试使用 while-loop
循环文件。
将 phrase = inFile.nextLine();
替换为:
while(inFile.hasNextLine()) // While there are lines in the file
phrase += inFile.nextLine(); // Add line to '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;
- 当
ch == ' '
时添加了switch-case
- 添加了
default-case
,因为它被认为是良好的编程实践。
关于java txt文件读取程序,仅读取txt文件的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21082129/