I have this code to count the number of * from a string entered. but I need to find it from an text file. Any idea?
import java.lang.String;
import java.io.*;
import java.util.*;
public class CountStars {
public static void main(String args[]) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String:");
String text = bf.readLine();
int count = 0;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c=='*' ) {
count++;
}
}
System.out.println("The number of stars in the given sentence are " + count);
}
}
We can write something as simple as below: