Java. Text Justification for left, right and center justified. Rules : Should no
ID: 3761559 • Letter: J
Question
Java. Text Justification for left, right and center justified.
Rules: Should not change line breaking or reduce spacing between words while performing justification. However, you can add or remove spaces at the start of each line. ** For right justification, the last word on each line ends at column 72. ** The program should take a third argument to specify justification.(Example command line: java example32 input.txt output.txt -left)
Input file.txt =
The quick red fox jumped over<------- (The) starts 2 spaces after column 0
the brown lazy dog. The quick,
red fox jumped over the brown,
lazy dog. Should I like dogs?
Yes, I like dogs a lot!<-------(Yes) starts 2 spaces after column 0
Now before we go any further, I am very new to Java. For lack of a better word, I am suffering in trying to grasp things many people find relatively easy like this example. Sometimes I feel like I'm too dumb to get it or I just don't get it at all. Anyways, here's what I tried for left justification. Only problem is, the words after the periods (.) are double spaced and my method takes away that double space. I'm sure there are many ways to do this, I just don't get it. :/
import java.util.*;
import java.io.*;
public class Example32{
public static void main(String[]args)throws FileNotFoundException{
Scanner input = new Scanner(new File("C:\Users\John Doe\Documents\Example3p2.txt"));
while(input.hasNextLine()){
String text = input.nextLine();
leftJustify(text,System.out);
}
}
public static void leftJustify(String text, PrintStream output){
Scanner data = new Scanner(text);
if(data.hasNext()){
output.print(data.next());
while(data.hasNext()){
output.print(" "+data.next());
}
}
output.println();
}
}
Output is as follows:
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!
If anyone can help me, please explain to me explicitly how and why such methods are used. Any help is greatly appreciated. Thank you.
Explanation / Answer
Java. Text Justification for left, right and center justified. Rules : Should no
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.