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

Please Help Using the programming rubric, create a program that performs the fol

ID: 3550792 • Letter: P

Question

Please Help


Using the programming rubric, create a program that performs the following task: Input: Prompt the user to specific which alignment he/she wants the data to be formatted. A data file containing data on each line. Each line is to be treated as an individual entity with no more than 80 characters on a line. Specifications: Right Aligned Contents of each line is aligned on the right side Left Aligned Contents of each line is aligned on the left side Center Aligned Contents of each line is aligned in the center Full (Justify) Aligned Contents of each line are aligned so that it takes up the entire length of the line. Note: Do not alter the individual words on the line. Output the results of the program to a file as well as to the monitor.

Explanation / Answer






Formatted code at https://gist.github.com/bitwiser/9133010


import java.io.*;

import java.util.Scanner;

import java.util.StringTokenizer;

class MainFile{

final static int LENGTH = 80;

final static char EMPTY = '_';

final static String PATH = "C:\temp\hw.txt";

public static void main(String[] args){

try{

char ch;

String out = "";

FileInputStream fstream = new FileInputStream(PATH);

// Get the object of DataInputStream

DataInputStream in = new DataInputStream(fstream);

Scanner in2 = new Scanner(System.in);

BufferedReader br = new BufferedReader(new InputStreamReader(in));

String strLine;

while ((strLine = br.readLine()) != null){

// Print the content on the console

System.out.println(strLine);

System.out.print("Enter alignment(l,r,c,j): ");

ch = in2.next().charAt(0);

ch = Character.toLowerCase(ch);

System.out.println(ch);

switch(ch){

case 'l':

out = strLeft(strLine);

break;

case 'r':

out = strRight(strLine);

break;

case 'c':

out = strCenter(strLine);

break;

case 'j':

out = strJustify(strLine);

break;

default: break;

}

System.out.println(out);

}

in.close();

in2.close();

}

catch(Exception e){

System.err.println("Error: " + e.getMessage());

}

}


private static String strJustify(String strLine) {

// TODO Auto-generated method stub

StringTokenizer st = new StringTokenizer(strLine);

if(st.countTokens()==1){

return strLeft(strLine);

}

String[] tok = new String[st.countTokens()];

int i=0,tot=0;

while(st.hasMoreElements()){

tok[i++] = st.nextToken();

tot+=tok[i-1].length();

}

String out = "";

System.out.println(tok.length);

int t = (LENGTH-tot)/(tok.length-1);

for(int j=0;j<tok.length;j++){

if(j==tok.length-1){

out+=tok[j];

return out;

}

out = out+tok[j];

for(int k=0;k<t;k++)

out+=EMPTY;

}

return out;

}


private static String strCenter(String strLine) {

// TODO Auto-generated method stub

String out = "";

int l = strLine.length();

int i;

if(l<=LENGTH){

int m = (LENGTH-l)/2;

//System.out.println(l);

for(i=0;i<m;i++){

//System.out.print(i);

out = out+EMPTY;

}

out = out+strLine;

for(i=i+l;i<LENGTH;i++){

out = out+EMPTY;

}

}

return out;

}


private static String strRight(String strLine) {

// TODO Auto-generated method stub

String out = "";

int l = strLine.length();

int i;

if(l<=LENGTH){

//System.out.println(l);

for(i=0;i<LENGTH-l;i++){

//System.out.print(i);

out = out+EMPTY;

}

out = out+strLine;

}

return out;

}


private static String strLeft(String strLine) {

// TODO Auto-generated method stub

String out = "";

int l = strLine.length();

int i;

if(l<=LENGTH){

//System.out.println(l);

out = out+strLine;

for(i=0;i<LENGTH-l;i++){

//System.out.print(i);

out = out+EMPTY;

}

}

return out;

}

}