Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class TextAlignment {
- public static void main(String[] args) {
- List<String> align = Arrays.asList("left", "right", "centre", "justify");
- if (args.length != 3 || !align.contains(args[1].toLowerCase())
- || !args[2].matches("[0-9]+")
- || Integer.parseInt(args[2]) < 1
- ) {
- System.out.println("usage: java TextAlignment <filename> <alignmentType> <lineLength>");
- // System.exit(0);
- return;
- }
- String fileName = args[0];
- String alignment = args[1];
- int lineLength = Integer.parseInt(args[2]);
- String[] input = readFile(fileName);
- try {
- // FileReader fileReader = new FileReader(newFile);
- // while ((ch = fileReader.read()) != -1)
- // paragaphs = paragaphs + (char) ch;
- switch (alignment.toLowerCase()) {
- case "left":
- for (int i = 0; i < input.length; i++) {
- printLeftOutput(input[i], lineLength);
- }
- // printLeftOutput(paragaphs, lineLength);
- break;
- case "right":
- for (int i = 0; i < input.length; i++) {
- printRightOutput(input[i], lineLength);
- }
- break;
- case "centre":
- for (int i = 0; i < input.length; i++) {
- printCenterOutput(input[i], lineLength);
- }
- break;
- case "justify":
- for (int i = 0; i < input.length; i++) {
- printJustifyOutput(input[i], lineLength);
- }
- break;
- }
- // fileReader.close();
- } catch (Exception e) {
- // System.out.println(e);
- }
- }
- // ---------------------Read File------------------------------------//
- public static String[] readFile(String filename) {
- try {
- // try to read from the specified file and store paragraphs (lines of text
- // with new-line at end) in list and convert list to array for return
- FileReader fr = new FileReader(filename);
- BufferedReader bfr = new BufferedReader(fr);
- ArrayList<String> content = new ArrayList<String>();
- String paragraph = null;
- while ((paragraph = bfr.readLine()) != null) {
- content.add(paragraph);
- }
- String[] paragraphs = new String[content.size()];
- for (int i = 0; i < content.size(); i++) {
- paragraphs[i] = content.get(i);
- }
- return paragraphs;
- } catch (FileNotFoundException e) {
- System.out.println("File not found: " + e.getMessage());
- } catch (IOException e) {
- System.out.println("I/O Ooops: " + e.getMessage());
- }
- // If an exception occurred we will get to here as the return statement above
- // was not executed
- // so setup a paragraphs array to return which contains the empty string
- String[] paragraphs = new String[1];
- paragraphs[0] = "";
- return paragraphs;
- }
- // ----------------Align Right------------------------------//
- public static void printRightOutput(String text, int lgt) {
- StringBuilder alignedText = new StringBuilder();
- String[] words = text.split("\\s");
- StringBuilder currentLine = new StringBuilder();
- int currentLineLength = 0;
- for (String word : words) {
- int wordLength = word.length();
- if (currentLineLength + wordLength <= lgt) {
- // Add word to the current line with a space
- currentLine.append(word).append(" ");
- currentLineLength += wordLength + 1;
- } else if(wordLength > lgt){ // If word is longer than line length
- // Add the current line to the aligned text
- if(currentLineLength > 0){
- alignedText.append(rightAlign(currentLine.toString().trim(), lgt));
- alignedText.append("\n");
- }
- // Add the new word to the aligned text
- alignedText.append(word.toString());
- alignedText.append("\n");
- currentLine.setLength(0);
- currentLineLength = 0;
- }
- else {
- // Start a new line and add word to it
- alignedText.append(rightAlign(currentLine.toString().trim(), lgt));
- alignedText.append("\n");
- currentLine.setLength(0);
- currentLineLength = 0;
- currentLine.append(word).append(" ");
- currentLineLength += wordLength + 1;
- }
- }
- if(currentLineLength > 0){
- // Add the last line
- alignedText.append(rightAlign(currentLine.toString().trim(), lgt)).append("\n");
- }
- System.out.print(alignedText.toString());
- }
- public static String rightAlign(String text, int lineLength) {
- // Calulate the number of spaces to add before the words in same line when no
- // more words fit in line
- int spacesToAdd = lineLength - text.length();
- if (spacesToAdd <= 0) {
- return text;
- }
- return " ".repeat(spacesToAdd) + text;
- }
- // ---------------------Align Left--------------------------//
- public static void printLeftOutput(String text, int lgt) {
- String[] words = text.split("\\s");
- StringBuilder alignedText = new StringBuilder();
- int currentLineLength = 0;
- for (String word : words) {
- if (currentLineLength + word.length() <= lgt) {
- // Add word to the current line with a space
- alignedText.append(word).append(" ");
- currentLineLength += word.length() + 1;
- }else if(word.length() > lgt){ // If word is longer than line length
- // Add the current line to the aligned text
- if(currentLineLength > 0){
- alignedText.append("\n");
- }
- // Add the new word to the aligned text
- alignedText.append(word.toString());
- alignedText.append("\n");
- currentLineLength = 0;
- }
- else {
- // Start a new line and add word to it
- alignedText.append("\n").append(word).append(" ");
- currentLineLength = word.length() + 1;
- }
- }
- System.out.println(alignedText.toString().trim());
- }
- // -------------------Align Center------------------------//
- public static void printCenterOutput(String text, int lgt) {
- StringBuilder alignedText = new StringBuilder();
- String[] words = text.split("\\s");
- StringBuilder currentLine = new StringBuilder();
- int currentLineLength = 0;
- for (String word : words) {
- int wordLength = word.length();
- if (currentLineLength + wordLength <= lgt) {
- // Add word to the current line with a space
- currentLine.append(word).append(" ");
- currentLineLength += wordLength + 1;
- }else if(wordLength > lgt){ // If word is longer than line length
- // Add the current line to the aligned text
- if(currentLineLength > 0){
- alignedText.append(centerAlign(currentLine.toString().trim(), lgt));
- alignedText.append("\n");
- }
- // Add the new word to the aligned text
- alignedText.append(word.toString().trim());
- alignedText.append("\n");
- currentLine.setLength(0);
- currentLineLength = 0;
- }
- else {
- // Start a new line and add word to it
- alignedText.append(centerAlign(currentLine.toString().trim(), lgt));
- alignedText.append("\n");
- currentLine.setLength(0);
- currentLine.append(word).append(" ");
- currentLineLength = wordLength + 1;
- }
- }
- // Add the last line
- if(currentLineLength > 0){
- alignedText.append(centerAlign(currentLine.toString().trim(), lgt)).append("\n");
- }
- System.out.println(alignedText.toString());
- }
- public static String centerAlign(String text, int lineLength) {
- // Calulate the number of spaces to add between the words of the same line when
- // no more words fit in line
- int spacesToAdd = lineLength - text.length();
- int rightSpaces = spacesToAdd / 2;
- int leftSpaces = spacesToAdd - rightSpaces;
- return " ".repeat(leftSpaces) + text + " ".repeat(rightSpaces);
- }
- // --------------------Align Justify---------------------------//
- public static void printJustifyOutput(String text, int lineLength) {
- String input = text.trim();
- StringBuilder justifiedText = new StringBuilder();
- StringBuilder currentLine = new StringBuilder();
- int lineChars = 1;
- currentLine.append(input.charAt(0));
- ++lineChars;
- // Loop through the input string
- for (int i = 1; i < input.length() - 1; i++) {
- char prevChar, currChar, nextChar;
- prevChar = input.charAt(i - 1);
- currChar = input.charAt(i);
- nextChar = input.charAt(i + 1);
- // Check if the current character is the last character of the input string
- boolean extraChar = false;
- // Check if the current character is a space and the line contains only one character
- if (lineChars == 1 && currChar == ' ') {
- continue;
- }
- // Check if the current character is a space and the next character is a space
- if (lineLength > lineChars) {
- currentLine.append(currChar);
- ++lineChars;
- } else { // If the line contains the maximum number of characters
- if (prevChar != ' ' && currChar != ' ' && nextChar != ' ') {
- currentLine.append("-");
- extraChar = true;
- } else if (prevChar == ' ' && currChar != ' ' && nextChar != ' ') {
- currentLine.append(" ");
- extraChar = true;
- } else if (prevChar != ' ' && currChar != ' ' && nextChar == ' ') {
- currentLine.append(currChar);
- } else if (prevChar != ' ' && currChar == ' ' && nextChar != ' ') {
- currentLine.append(currChar);
- } else {
- currentLine.append(currChar);
- }
- justifiedText.append(currentLine.toString().trim()).append("\n");
- currentLine.setLength(0);
- lineChars = 1;
- if (extraChar) {
- currentLine.append(currChar);
- ++lineChars;
- }
- }
- }
- currentLine.append(input.charAt(input.length() - 1));
- if (currentLine.length() > 0) {
- justifiedText.append(currentLine.toString().trim()).append("\n");
- }
- System.out.println(justifiedText.toString().trim());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement