Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will read a file called message.txt that contains ten lines

ID: 3633569 • Letter: W

Question

Write a program that will read a file called message.txt that contains ten lines of text. The program should determine the total number of words in the text, the number of vowels in each line and the average number of words per line.

Steps in solving this problem:

1. Create message.txt in notepad.
2. Write ten lines of text making sure to press enter at the end of each line.
3. Using the problem solving process (IPO chart, menu planning, pseudo code for each menu option) analyze this problem.
4. Design a solution - write java on paper for each method Be sure to properly indent and internally document the program (include comments).

IMPORTANT: I am using java 1.4.2 so please ensure it works with this version and not for newer versions.

Explanation / Answer

Make sure Message.txt file exists in proper path. "c:Message.txt" (c drive).


//TextParse.java
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TextParse
{
    public static void main(String[] args)
    {
        String[] inputData = new String[10];
        String regex = "[aeiou]";
        int totalNumWords = 0;
        int nCount = 0;
        try
        {
            FileInputStream inputfile = new FileInputStream("c:\Message.txt");
            DataInputStream in = new DataInputStream(inputfile);
            BufferedReader input = new BufferedReader(new InputStreamReader(in));
            String strLine;          
            while ((strLine = input.readLine()) != null)
            {
                inputData[nCount] = strLine;
                nCount++;
                if(nCount == 10)
                    break;
            }
            in.close();          
            nCount = 0;          
            for(int i=0;i<10;i++)
            {
                String[] words = inputData[i].split(" ");
                totalNumWords = totalNumWords + words.length;
                Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
                int vowelcount = 0;
                Matcher match = pattern.matcher(inputData[i]);        
                while (match.find())
                {
                  vowelcount++;
                }
                System.out.println("No of vowels in line#"+(nCount+1)+" : "+vowelcount);
                nCount++;          
            }
            System.out.println(" Total number of words : " + totalNumWords);
            System.out.println(" Average number of words per line : "+(totalNumWords/10));
        }
        catch (Exception e)
        {
        }
    }  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote