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

Create a text file from a source on the Internet (i.e. song lyrics, article, poe

ID: 3849771 • Letter: C

Question

Create a text file from a source on the Internet (i.e. song lyrics, article, poem, etc.) Prompt the user for the filename, then open the file. Remember to use/instead of . For example: c: /data/myfile.txt or/javaData/song.txt Read the text file into a String or array of chars in main() Write the following methods to analyze the data read from the file: a. void DisplayString(string) b. void DisplayStringCharRemoved(string, char) c. void DisplayAllUpper(string) d. void DisplayAllLower(string) e. int CountLowerAlpha(string) f. int CountUpperAlpha(string) g. int CountAllAlpha(string) h. int CountNonAlpha(string) i. void DisplayReverseString(string) This will rely heavily on looking at each character in the string with CharAt(). See the sample code I've posted for ideas. Output: Please enter filename: song.txt Original File: 123XXyyxxYYxxyy File with 'x' removed: 123XXyyYYyy All Upper: XXYYXXYYXXYY All Lower: Xxyyxxyyxxyy Number of Alpha: 12 Number of Lower: 8 Number of Upper: 4 Number of Non-Alpha: 3 Reverse String: yyxxYYxxyyXX321

Explanation / Answer

Code:
import java.util.*;
import java.io.*;

public class Test{
    public static void main(String[] args) throws FileNotFoundException{
        String theString = "";
        Scanner sc = new Scanner(System.in);
        int all_alpha, low_alpha, upper_alpha, non_alpha;

        // reading the filename
        System.out.println("Enter the filename: ");
        String fname = sc.next();
        File file = new File(fname);
        sc = new Scanner(file);
        // iterating throuch each line in file and appending to the variable
        // theString
        while(sc.hasNextLine()){
                theString = theString + " " + sc.nextLine();
        }
      
        displayString(theString);
        displayStringCharRemoved(theString, 'E');
        displayAllUpper(theString);
        displayAllLower(theString);
        low_alpha = countLowerAlpha(theString);
        upper_alpha = countUpperAlpha(theString);
        all_alpha = countAllAlpha(theString);
        non_alpha = countNonAlpha(theString);
        System.out.println("Number of alpha: "+all_alpha);
        System.out.println("Number of lower: "+low_alpha);
        System.out.println("Number of upper: "+upper_alpha);
        System.out.println("Number of Non-Alpha: "+non_alpha);
        displayReverseString(theString);

    }

    // displaying the given file contents
    static void displayString(String str)
    {
        char[] charArray = str.toCharArray();

        System.out.println(" Given file contents are:");
        for(int i = 0; i < charArray.length; i++)
            System.out.print(charArray[i]);
        System.out.println();
    }
    // display string char removed
    static void displayStringCharRemoved(String str, char ch)
    {
        char[] charArray = str.toCharArray();

        System.out.println(" Given file contents after removing the character " + ch + " is:");
        for(int i = 0; i < charArray.length; i++)
            if(charArray[i] != ch)
                System.out.print(charArray[i]);
        System.out.println();
    }

    //converting all the characters to uppercase
    static void displayAllUpper(String str)
    {
        System.out.println(" Given file contents after converting everything to uppercase is: ");
        System.out.println(str.toUpperCase());
    }

    //converting all the characters to lowercase
    static void displayAllLower(String str)
    {
          System.out.println(" Given file contents after converting everything to lowercase is: ");
          System.out.println(str.toLowerCase());
    }

    // reversing the string
    static void displayReverseString(String str)
    {
        StringBuilder str2 = new StringBuilder();
        str2.append(str);

System.out.println(" File contents after reversing are: ");
System.out.println(str2.reverse());
    }

    // count all lower alphabets
    static int countLowerAlpha(String str)
    {
char[] charArray = str.toCharArray();
int countLower = 0;

for(int i = 0; i < charArray.length; i++)
    if(charArray[i]>='a' && charArray[i]<='z')
      countLower++;
return countLower;
    }

    // count all upper alphabets
    static int countUpperAlpha(String str)
    {
char[] charArray = str.toCharArray();
int countUpper = 0;

for(int i = 0; i < charArray.length; i++)
    if(charArray[i]>='A' && charArray[i]<='Z')
      countUpper++;
return countUpper;
    }

    // count all alphabets
    static int countAllAlpha(String str)
    {
        char[] charArray = str.toCharArray();
        int alpha = 0;

        for(int i = 0; i < charArray.length; i++)
            if((charArray[i]>='A' && charArray[i]<='Z') || (charArray[i]>='a' && charArray[i]<='z'))
                alpha++;
        return alpha;
    }

    // count non-alpha characters
    static int countNonAlpha(String str)
    {
        char[] charArray = str.toCharArray();
        int non_alpha = 0;

        for(int i = 0; i < charArray.length; i++)
            if (!( (charArray[i]>='A' && charArray[i]<='Z') || (charArray[i]>='a' && charArray[i]<='z') ))
                non_alpha++;
        return non_alpha;
    }

}

Execution and output:
Unix Terminal> cat myfile.txt
Old MACDONALD had a farm
E-I-E-I-O
And on his farm he had a cow
E-I-E-I-O
With a moo moo here
And a moo moo there
Here a moo, there a moo
Everywhere a moo moo
Old MacDonald had a farm
E-I-E-I-O


Unix Terminal> javac Test.java; java Test
Enter the filename:
myfile.txt

Given file contents are:

Old MACDONALD had a farm
E-I-E-I-O
And on his farm he had a cow
E-I-E-I-O
With a moo moo here
And a moo moo there
Here a moo, there a moo
Everywhere a moo moo
Old MacDonald had a farm
E-I-E-I-O

Given file contents after removing the character E is:

Old MACDONALD had a farm
-I--I-O
And on his farm he had a cow
-I--I-O
With a moo moo here
And a moo moo there
Here a moo, there a moo
verywhere a moo moo
Old MacDonald had a farm
-I--I-O

Given file contents after converting everything to uppercase is:

OLD MACDONALD HAD A FARM
E-I-E-I-O
AND ON HIS FARM HE HAD A COW
E-I-E-I-O
WITH A MOO MOO HERE
AND A MOO MOO THERE
HERE A MOO, THERE A MOO
EVERYWHERE A MOO MOO
OLD MACDONALD HAD A FARM
E-I-E-I-O

Given file contents after converting everything to lowercase is:

old macdonald had a farm
e-i-e-i-o
and on his farm he had a cow
e-i-e-i-o
with a moo moo here
and a moo moo there
here a moo, there a moo
everywhere a moo moo
old macdonald had a farm
e-i-e-i-o

Number of alpha: 140
Number of lower: 107
Number of upper: 33
Number of Non-Alpha: 54

File contents after reversing are:
O-I-E-I-E
mraf a dah dlanoDcaM dlO
oom oom a erehwyrevE
oom a ereht ,oom a ereH
ereht oom oom a dnA
ereh oom oom a htiW
O-I-E-I-E
woc a dah eh mraf sih no dnA
O-I-E-I-E
mraf a dah DLANODCAM dlO

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