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

By using Java program solve following program with a public static method named

ID: 3762376 • Letter: B

Question

By using Java program solve following program

with a public static method named justifyText that accepts two parameters; a Scanner representing a file as the first parameter, and an int width specifying the output text width. Your method writes the text file contents to the console in full justification form For example, if a Scanner is reading an input file containing the following text:

Four      score   and

seven               years ago         our

fathers brought             forth

    on this          continent

a         new        

nation.

Then a call to justifyText(input,30); will output the following to the console:

Four score and seven years ago
our fathers brought forth on
this continent a new nation.

Hint: Start by removing all the extra space from the text (a similar Exercise in text). Then calculate the number of spaces you need to add back in, to achieve the desired width, then System.out the text one line at a time. You may assume that we need to add only one or two spaces between words, and do not hyphenate words as other fancy programs might do. Keep it simple (well it's not that simple). And be certain that what you submit passes the Java compiler and does produce some output!!! A few points is infinitely better than a zero.....

Here's my test code:

public class Chapter6 {
   public static void main(String[] args) throws FileNotFoundException {
       File spaces = new File("spaces.txt");
       Scanner input = new Scanner(spaces);
       justifyText(input ,30);          
   }
   // Additional methods added below:
}

Explanation / Answer


import java.io.File;
import java.util.Scanner;

public class Chapter6 {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       try {
           File spaces = new File("spaces.txt");
           Scanner input = new Scanner(spaces);
           justifyText(input, 30);
       } catch (Exception e) {
           // TODO: handle exception
       }

   }

   /**
   * @param input
   * @param width
   */
   public static void justifyText(Scanner input, int width) {
       try {

           String totalString = "";
           while (input.hasNextLine()) {
               totalString += input.nextLine();

           }

           totalString = totalString.replaceAll("\s+", " ").trim();

           printString(totalString, 30);

           // System.out.println("total String==>"+totalString.length());
           input.close();
       } catch (Exception e) {
           e.printStackTrace();
       }

   }

   /**
   * @param inStr
   * @param width
   */
   public static void printString(String inStr, int width) {

       int min = 0;
       while (true) {
           // i++;
           // if(i==5)break;

           if (inStr.length() <= width) {
               width = inStr.length();
           }

           String tempStr = inStr.substring(min, width);

           int lastIndex = tempStr.lastIndexOf(" ");

           if (lastIndex == 0) {
               System.out.println(inStr.trim());
               break;
           }

           String strLine = inStr.substring(min, lastIndex);
           inStr = inStr.substring(lastIndex, inStr.length());

           System.out.println(strLine.trim());

       }
   }

}

OUTPUT

Four score andseven years ago
ourfathers brought forth on
this continenta new
nation.