
Ravi Raj,Vijay45Shankar,234
Guna,345,Meet me,654,Cow
Read file contents and Print the no.of words and Numbers./p>
Thanks, Dinesh Ram

The given code read the file content and print the no.of words and Numbers.
import java.io.*;
class CountWordsAndNumberFromFile{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new FileReader(new File("c:/A.txt")));
String str="",st="";
while((str=br.readLine())!=null){
st+=str+" ";
}
String s=st.replace(","," ");
String array[]=s.split(" ");
int word=0,num=0;
for(int i=0;i<array.length;i++){
if (array[i].matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
num++;
} else {
word++;
}
}
System.out.println("Number of numbers: "+num);
System.out.println("Number of words: "+word);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.