Answer in Java CPS 180 - Lab Assignment #9 Methods and File I/O – Read Text File
ID: 3848645 • Letter: A
Question
Answer in Java
CPS 180 - Lab Assignment #9
Methods and File I/O – Read Text File and Analyze It
Assigned: 06/12/17 Due: 06/19/17
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:
void DisplayString(string)
void DisplayStringCharRemoved(string, char)
void DisplayAllUpper(string)
void DisplayAllLower(string)
int CountLowerAlpha(string)
int CountUpperAlpha(string)
int CountAllAlpha(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:
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
Hi,
Please see below the class and sample output
Please comment for any queries
Thanks.
File_Reader.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import javax.swing.text.AttributeSet.CharacterAttribute;
public class File_Reader {
public static Scanner scanner = new Scanner(System.in);
/**
* @param args
*/
public static void main(String[] args) {
//Reading the file name from user
System.out.println("Please enter the fileName: ");
String fileName = scanner.nextLine();
String fileContent = readFile(fileName);
DisplayString(fileContent);
DisplayStringCharRemoved(fileContent, 'x');
DisplayAllUpper(fileContent);
DisplayAllLower(fileContent);
int count1 = CountAllAlpha(fileContent);
System.out.println("Number of Alpha:"+count1);
int countlower = CountLowerAlpha(fileContent) ;
System.out.println("Number of Lower: "+countlower);
int countupper = CountUpperAlpha(fileContent) ;
System.out.println("Number of Upper: "+countupper);
int countnonAlpha= CountNonAlpha(fileContent) ;
System.out.println("Number of Non-Alpha: "+countnonAlpha);
DisplayReverseString(fileContent);
}
public static String readFile( String fileName){
BufferedReader br = null;
FileReader fr = null;
String fileContent = "";
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
fileContent = fileContent + sCurrentLine ;
}
} catch (IOException e) {
e.printStackTrace();
}
return fileContent;
}
/**
* to display the string
* @param string
*/
public static void DisplayString(String string){
System.out.println("Original File:");
System.out.println(string);
}
/**
* DisplayStringCharRemoved
* @param string
* @param letter
*/
public static void DisplayStringCharRemoved(String string, char letter){
String actual = string;
String replaced = actual.replaceAll(String.valueOf(letter),"");
System.out.println("File with '"+letter+"' removed:"+replaced);
}
/**
* DisplayAllUpper
* @param string
*/
public static void DisplayAllUpper(String string){
String actual = string;
String upper = actual.toUpperCase();
System.out.println("All Upper:"+upper);
}
/**
* DisplayAllLower
* @param string
*/
public static void DisplayAllLower(String string){
String actual = string;
String lower = actual.toLowerCase();
System.out.println("All Lower:"+lower);
}
/**
* CountAllAlpha
* @param string
* @return
*/
public static int CountAllAlpha(String string){
int counter =0;
for(int i=0; i<string.length();i++){
if( '1' != string.charAt(i)
&& '2' != string.charAt(i)
&& '3' != string.charAt(i)
&& '4' != string.charAt(i)
&& '5' != string.charAt(i)
&& '6' != string.charAt(i)
&& '7' != string.charAt(i)
&& '8' != string.charAt(i)
&& '9' != string.charAt(i)
&& '0' != string.charAt(i)
){
counter++;
}
}
return counter;
}
/**
* CountLowerAlpha
* @param string
* @return
*/
public static int CountLowerAlpha(String string){
int counter =0;
for(int i=0; i<string.length();i++) {
if(Character.isLowerCase(string.charAt(i) )){
counter++;
}
}
return counter;
}
/**
* CountUpperAlpha
* @param string
* @return
*/
public static int CountUpperAlpha(String string){
int counter =0;
for(int i=0; i<string.length();i++) {
if(Character.isUpperCase(string.charAt(i) )){
counter++;
}
}
return counter;
}
/**
* CountNonAlpha
* @param string
* @return
*/
public static int CountNonAlpha(String string){
int counter =0;
for(int i=0; i<string.length();i++){
if( '1' == string.charAt(i)
|| '2' == string.charAt(i)
|| '3' == string.charAt(i)
|| '4' == string.charAt(i)
|| '5' == string.charAt(i)
|| '6' == string.charAt(i)
|| '7' == string.charAt(i)
|| '8' == string.charAt(i)
|| '9' == string.charAt(i)
|| '0' == string.charAt(i)
){
counter++;
}
}
return counter;
}
public static void DisplayReverseString(String string){
String sb = new StringBuilder(string).reverse().toString();
System.out.println("Reverse String: "+sb.toString());
}
}
song.txt
123XXyyxxYYxxyy
Sample output:
Please enter the fileName:
D:CH_WS EwProjectsong.txt
Original File:
123XXyyxxYYxxyy
File with 'x' removed:123XXyyYYyy
All Upper:123XXYYXXYYXXYY
All Lower:123xxyyxxyyxxyy
Number of Alpha:12
Number of Lower: 8
Number of Upper: 4
Number of Non-Alpha: 3
Reverse String: yyxxYYxxyyXX321
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.