Submit Chapter6.java with a public static method named that accepts two paramete
ID: 3762432 • Letter: S
Question
Submit Chapter6.java with a public static method named that accepts two parameters; a representing a file as the first parameter, and an width specifying the output text width. Your method writes the text file contents to the console in full justification form (I'm sure you've seen this in Microsoft Word ). For example, if a is reading an input file containing the following text: Then a call to will output the following to the console: 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..... To run above example, I had the following file in my Eclipse project to read these data from the text:Explanation / Answer
/**The java program Chapter6 that opens a input file "input.txt" file and calls the metod justifyText and prints the words from the text of a width 30*/
//Chapter6.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Chapter6
{
public static void main(String[] args)
{
Scanner scanner=null;
final int width=30;
try
{
//open an input text file "input.txt"
scanner=new Scanner(new File("input.txt"));
//calling justifyText method
justifyText(scanner, width);
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
}
/**The method justifyText takes an input parameters
* Scanner object and widht and then prints text line of widht
* 30*/
public static void justifyText(Scanner scanner, int width)
{
StringBuilder filetext=new StringBuilder();
//read text file and remove spaces with single space
while(scanner.hasNextLine())
{
String line=scanner.nextLine().trim().replaceAll(" +", " ")+" ";
//append string to string builder object filetext
filetext.append(line);
}
//create a string objec of StringBuilder object filetext
String file=new String(filetext);
//call split method to split the words of space
String[] text=file.split(" ");
//set count to 1
int count=1;
//set space to zero
int space=0;
for (int index = 0; index < text.length-1; index++)
{
//count the size of the text
count+=text[index].length();
//add the space to count and check if size is less than or =30
if(count+space<=30)
{
//print text
System.out.printf(text[index]+" ");
//increment space by one
space++;
}
else
{
//print text and set single space
System.out.printf(text[index]+" ");
//print a new line
System.out.println();
//reset count to zero
count=0;
}
}
//print last word
System.out.println(text[text.length-1]);
}
}
---------------------------------------------------------------------------------------------------------
input text file
Four score and
seven years ago our
fathers brought forth
on this continent
a new
nation.
--------------------------------------
Sample output:
Four score and seven years ago
our fathers brought forth on
this continent a new nation.