By java language.. please be clear with your answer. Create a text file from a s
ID: 3849022 • Letter: B
Question
By java language.. please be clear with your answer. Create a text file from a source on the Internet song lyrics, article, poem, etc.) 2 Prompt the user for the filename, then open the file. Remember to use instead of For example: c:/data/myfile.txt /java Data/song.txt Read the text file into a String or array of chars 4 write the following methods to analyze the data read from a void DisplayString(string) b void DisplayStringCharRemoved (string, char) void DisplayAllUpper(string) d void DisplayAllLower(string) int CountLowerAlpha (string E int CountUpperAlpha (string) int CountAIIAlpha(string) int CountNonAlpha(string) 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: 123XXyy YYyy All Upper: XXYYXXYYXXYY All Lower Number of Alpha 12 Number of Lower: 8 Number of Upper: 4 Number of Non-Alpha: 3 Reverse string: yyxxYYxxyyXX321
Explanation / Answer
import java.io.*;
import java.util.*;
public class Sample
{
public static String content="";
//Method for displaying string
public static void DisplayString(String str)
{
System.out.println("Original File : ");
System.out.println(content);
}
//Method for displaying string with removing the given character
public static void DisplayStringCharRemoved(String str,char c)
{
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)!=c)
System.out.print(str.charAt(i));
}
System.out.println();
}
//Method for displaying all upper letters from str
public static void DisplayAllUpper(String str)
{
System.out.println("All Upper : ");
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>='A' && str.charAt(i)<='Z')
System.out.print(str.charAt(i));
}
System.out.println();
}
//Method for displaying all lower letters from str
public static void DisplayAllLower(String str)
{
System.out.println("All Lower : ");
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>='a' && str.charAt(i)<='z')
System.out.print(str.charAt(i));
}
System.out.println();
}
//Method for couting all lower alpha from given string
public static void CountLowerAlpha(String str)
{
int count=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>='a' && str.charAt(i)<='z')
count++;
}
System.out.println("Number of Lower : "+ count);
}
//Method for couting all Upper alpha from given string
public static void CountUpperAlpha(String str)
{
int count=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>='A' && str.charAt(i)<='Z')
count++;
}
System.out.println("Number of Upper : "+ count);
}
//Method for couting all alpha from given string
public static void CountAllAlpha(String str)
{
int count=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>='a' && str.charAt(i)<='z' || str.charAt(i)>='A' && str.charAt(i)<='Z')
count++;
}
System.out.println("Number of Alpha : "+ count);
}
//Method for couting non alpha from given string
public static void CountNonAlpha(String str)
{
int count=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>='a' && str.charAt(i)<='z' || str.charAt(i)>='A' && str.charAt(i)<='Z')
count++;
}
int ans=str.length()-count;
System.out.println("Number of Non-Alpha : "+ ans);
}
//Method for printing reverse string
public static void ReverseString(String str)
{
System.out.println("Reverse String :");
for(int i=str.length()-1;i>=0;i--)
System.out.print(str.charAt(i));
System.out.println();
}
public static void main(String args[])
{
// The name of the file to open.
System.out.println("Enter the name of file to open");
Scanner s=new Scanner(System.in);
String fileName = s.next();//"input.txt";
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
content=bufferedReader.readLine();
// Always close files.
bufferedReader.close();
//Calling all methods
DisplayString(content);
System.out.println("File with 'x' removed :");
DisplayStringCharRemoved(content,'x');
DisplayAllUpper(content);
DisplayAllLower(content);
CountAllAlpha(content);
CountNonAlpha(content);
CountLowerAlpha(content);
CountUpperAlpha(content);
ReverseString(content);
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '"+ fileName + "'");
}
}
}
Output :
Enter the name of file to open
input.txt
Original File :
ASDG1232xxXXbbbbb
File with 'x' removed :
ASDG1232XXbbbbb
All Upper :
ASDGXX
All Lower :
xxbbbbb
Number of Alpha : 13
Number of Non-Alpha : 4
Number of Lower : 7
Number of Upper : 6
Reverse String :
bbbbbXXxx2321GDSA
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.