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

Java. Word processing program to reformat a text input, complete with capitaliza

ID: 3761308 • Letter: J

Question

Java. Word processing program to reformat a text input, complete with capitalization and correct number of spacing???

Spacing and Capitalization All input text should be processed by the following rules:
There should be exactly one space between each word.

There should be exactly one space after a comma (,) before the next word. Do not insert spaces unless there is an additional word on the same line.

The word “I” (capital i), should always be capitalized.

There should be exactly two spaces after a period (.), question mark (?), or exclamation point (!) before the next word. Do not insert spaces unless there is an additional word on the same line.

The first word in each sentence should be capitalized. This includes the first word in each paragraph and the first word after each period, question mark, or exclamation point.

Example input:
the quick red fox jumped over the brown lazy dog.the quick, red fox jumped over the brown ,lazy dog. should i like dogs? yes, I like dogs a lot!

Issues:
the quick __red fox jumped over the brown lazy dog.the quick, red fox jumped over the brown__,lazy dog._should i like dogs?_yes, I like dogs a lot!

Expected output:
The quick red fox jumped over the brown lazy dog. The quick, red fox jumped over the brown, lazy dog. Should I like dogs? Yes, I like dogs a lot!

Explanation / Answer

Java code:

//Header file section
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;

//main class
public class SpaceAndCapitalizationDemo
{
public static String readFileData(String filename) throws IOException
{
  Scanner br=new Scanner(new FileReader(filename)) ;
  
  String line="";
  
  while(br.hasNextLine())
  {
   line+=br.nextLine();   
  }
  
  br.close();
  return spaces(line);
  
}
private static String spaces(String content)
{
   StringTokenizer str = new StringTokenizer(content, ".!?", true);
   int index = 0;
   String s[]=new String[20];
  
   while(str.hasMoreTokens())
   {     
      s[index] = str.nextToken().trim();     
      s[index] = String.valueOf(s[index].charAt(0)).toUpperCase()+ s[index].substring(1);
      s[index] =s[index].replaceAll("( )+", " ");
      String tokens[]=s[index].split(" ");
      String strn = "";
      for(String o:tokens)
      {
       if(o.equals("i"))
        o = String.valueOf(o.charAt(0)).toUpperCase()+ o.substring(1);
       if(o.charAt(0)==',')
       {
        o = o.substring(1);
        strn = strn+", "+o;
       }
       else
       {
        strn = strn+" "+o;
       }
      
      }
      s[index] = strn;     
      index++;   
   }
   String result="";
   for(int i=0;i<index;i=i+2)
    result +=s[i].trim()+s[i+1].trim()+" ";
  
  return result;
}
//main method
public static void main(String arg[]) throws IOException
{
  
   String value = readFileData("inputData.txt");
   System.out.println(value);
  
}
}

Data in file "inputData.txt":

the quick red fox jumped over the brown lazy dog.the quick, red fox jumped over the brown ,lazy dog. should i like dogs? yes, I like dogs lot!

Output:

The quick red fox jumped over the brown lazy dog. The quick, red fox jumped over the brown, lazy dog. Should I like dogs? Yes, I like dogs lot!

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