I wrote a program that reads the number of times the letters a,e,s, and t and spaces occur in a txt file, and it currently works, but only reads the first line of the txt file. How do I make my program read all of the lines in a txt file, and then output the number of times these letters are used? Thank you for your time and help.
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;
}
}
}
}
You could try looping over the file with a while-loop
.
Replace phrase = inFile.nextLine();
with:
while(inFile.hasNextLine()) // While there are lines in the file
phrase += inFile.nextLine(); // Add line to 'phrase'
Don't forget to initialize String phrase
with an empty string:
String phrase = "";
Edit: Your final code should look like this, with some changes, listed at the end of this answer.
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;
}
}
Changes:
Scanner scan = new Scanner(System.in);
since you don't use it. if (phrase.equalsIgnoreCase("quit")) break;
switch-case
when ch == ' '
default-case
since it is considered good programming practice. Of course, You should use while
in order the wrap inFile.nextLine()
to produce you lines. But since you only use it once it only producing you the first line.
Try wrapping you code with this:
while ((phrase = inFile.nextLine()) != null) {
// Here code
}
You need to read the next line to String "phrase". Add
phrase = inFile.nextLine();
to the end of the while loop.
The Line
phrase = inFile.nextLine();
is out of your while loop. Hence it's getting executed only once. add it inside your while loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.