Question: I have to write a java code that produces a statistic from any text file as input. It should outprint how many 1-letter, 2-letter, 3-letter...

I have to write a java code that produces a statistic from any text file as input. It should outprint how many 1-letter, 2-letter, 3-letter... above 13-letter words the text file has. The output should look like this when running the code:

Name of input text: XXX.txt

1- letter words: 4.7% (300 words) ?

2- letter words: 15.8% (1008 words)

...

13 and above letter ...

Look at my below code: unfortunately I have problems inputing a text file. So how do I actually manage to input any txt file that is in the same folder as this program in the java editor? Lets assume the text is in same folder and the name is XXX.txt also how could i modify the below code to avoid using BufferedReader. My prof mentioned I should use useDelimiter(...) to ignore apostrophes ect. for the length of the word.

import java.io.*;

public class Authorship

{

public static void main(String[] args)throws Exception

{

File file = new File("~/workspace/pset6/romeoAndJuliet.txt");

BufferedReader br = new BufferedReader(new FileReader(file));

String st;

//To store the result of count of words of size 1,2,.....13 or above

int stringCount[] = new int[13];

int totalWords=0;

System.out.println("File data is " );

while ((st = br.readLine()) != null)

{

String data[] = st.split(" ");

for(String s:data)

{

System.out.print(s+" ");

//Ignore Apostrophes (" and ')

s =s.replaceAll("\"","");

s =s.replaceAll("\'","");

//If string length >=13 , keep in 13 size string group (Here array index starts from zero)

if(s.length()>=13) stringCount[12]++;

//else put it in its size group

else stringCount[s.length()-1]++;

//increase counter of totalword Count

totalWords++;

}

System.out.println();

}

System.out.println(" ");

for(int i=0;i<12;i++)

System.out.println("Proportion of "+(i+1)+"-letter words: " +((int)stringCount[i]*100/totalWords) + "% ("+stringCount[i]+" words)");

System.out.println("Proportion of 13-or above letter words: " +(stringCount[12]*0.01/totalWords) + "% ("+stringCount[12]+" words)");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!