Write a program that checks a text file for several formatting and punctuation m
ID: 3658695 • Letter: W
Question
Write a program that checks a text file for several formatting and punctuation matters. The program asks for the names of both an input file and an output file. It then copies all the text from the input file to the output file, but with the following two changes: (1) Any string of two or more blank characters is replaced by a single blank; (2) all sentences start with an uppercase letter. All sentences after the first one begin after either a period, a question mark, or an exclamation mark that is followed by one or more whitespace characters.Explanation / Answer
This is the exact answer to your que..
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;
public class FileOperation {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter the path for Source file");
String path1=sc.next();
File f1=new File(path1);
System.out.println("Enter the path for the Destination file");
String path2=sc.next();
File f2=new File(path2);
int count=0;
try
{
FileWriter fw=new FileWriter(f2);
BufferedWriter bw=new BufferedWriter(fw);
Scanner sc1=new Scanner(f1);
while(sc1.hasNext())
{
count ++;
String line=sc1.nextLine();
String newString="";
String[] lineArray = line.split(" ");
for(String s:lineArray)
{
if(!s.isEmpty())
{
newString +=s+" ";
//changeing 1st letter to Upper case
}
}
newString = newString.substring(0,1).toUpperCase()+newString.substring(1,newString.length());
if(count > 1)
{
newString = " ? "+newString;
}
bw.append(newString);
bw.append(" ");
}
bw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.