Skip to content

Instantly share code, notes, and snippets.

@bitwiser
Created February 21, 2014 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitwiser/9133010 to your computer and use it in GitHub Desktop.
Save bitwiser/9133010 to your computer and use it in GitHub Desktop.
/*
Specify the path of your input file in PATH variable
*/
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment