To do beautiful printing

Have you watched an English movie? Of course! Have you noticed the caption of end which tells us which role is played by whom? Maybe not. I have seen many English movies and some of them are very famous as they are awarded the Oscar award. I found an interesting thing, that is the more success or say famous of the movie, the longer time will for caption. I knew that people always care about the content only. I am too. But if you watch the last some seemed tedious part, you will find something interesting.

 

What I say interesting is the format of the captions. See the list below.  The “The Cast” is in the center/middle, the second is centered too. The role list is aligned to the right, and the actor list is aligned to the left, with a little bit width between them. All seems simple. but not at all. Just image one of the role's or actor's name is very long, then how to print thing like this? You may say that print it in another line with the rule that role name aligned to the right, actor name aligned to the left. That’s right. Actually, what we see from the movie is just the same like that. But how to break the line? I mean, the name is very long, and you want to print with another line, how to break the name? from which word? Which character? That's the problem!

 

The Cast

(in order of appearance)

 

                               Wladyslaw Sapilman           ADRIEN BRODY

                                                      Dorota           EMILIA FOX

                                                         Jurek          MICHAL ZEBROWSKI

                                                      Henryk          ED STOPPARD

                                                     Mother            MAUREEN LIPMAN

 

How to print something like this in our program? Here, is a example provided.

Java Code:

package noi;

import java.util.StringTokenizer;
import java.util.Vector;

public class BeautifulPrint {
    public static final int SCREENWIDTH = 80;
    public static final int ALIGN_LEFT = 1;
    public static final int ALIGN_CENTER = 2;
    public static final int ALIGN_RIGHT = 3;
   
    public void print(String str) {
        System.out.print(str);
    }
    public void println(String str) {
        System.out.println(str);
    }
    public void println() {
        System.out.println();
    }
   
    public void multiPrint(String str, int times) {
        for (int i = 0; i < times; i++) {
            print(str);
        }
    }
   
    public void simplePrint(String str, int width, int align) {
        int spaces = 0;
        if (align == BeautifulPrint.ALIGN_LEFT) {
            spaces = 0;
        } else if(align == BeautifulPrint.ALIGN_CENTER) {
            spaces = (width - str.length()) / 2;
        } else {
            spaces = width - str.length();
        }      
        multiPrint(" ", spaces);
        print(str);
    }
   
    public void formatPrint(String str, int width, int align) {
        StringTokenizer st = new StringTokenizer(str, " ");
        Vector wordVector = new Vector();
       
        while(st.hasMoreTokens()) {
            wordVector.addElement(st.nextToken());
        }
   
        int len = 0;
        int length = 0;
        int i = 0;
        String outStr = "";
        while (true) {                 
            len = outStr.length();
           
            String strTemp = (String)wordVector.get(i);
            length = strTemp.length();
           
            if (len + length > width) {
                if(len == 0) {
                    outStr += strTemp.substring(0, length + len - width);  
                    wordVector.set(i, strTemp.substring(length + len - width, length));
                }
                multiPrint(" ",(BeautifulPrint.SCREENWIDTH - width)/2);
                simplePrint(outStr, width, align);
                print("/n");
                outStr = "";
               
                continue;              
            } else {               
                outStr += strTemp + " ";
                i++;               
                if(i == wordVector.size()) {
                    multiPrint(" ",(BeautifulPrint.SCREENWIDTH - width)/2);
                    simplePrint(outStr, width, align);
                    print("/n");
                    outStr = "";
                    break;
                }
            }      
        }
    }  
    public String getMultiString(String str, int times) {
        String result = "";
        for (int i = 0; i < times; i++) {
            result += str;
        }
        return result;
    }
    /**
     * to print two-column strings
     * @param lstr string at the left part
     * @param rstr string at the right part
     * @param lWidth the width of left string
     * @param rWidth the width of right string
     * @param mWidth the width between left and right string
     */
    public void advancedFormatPrint(String lstr, String rstr, int lWidth, int rWidth, int mWidth) {
        StringTokenizer lst = new StringTokenizer(lstr, " ");      
        Vector lWordVector = new Vector();     
        while(lst.hasMoreTokens()) {
            lWordVector.addElement(lst.nextToken());
        }
       
        StringTokenizer rst = new StringTokenizer(rstr, " ");
        Vector rWordVector = new Vector();
        while(rst.hasMoreTokens()) {
            rWordVector.addElement(rst.nextToken());
        }
   
        int len = 0;
        int length = 0; // length of each word
        int i = 0;
        String outStr = "";
       
        Vector lStrVector = new Vector();  
       
        // process left word vector first
        while (true) {                 
            len = outStr.length();
           
            String strTemp = (String)lWordVector.get(i);
            length = strTemp.length();
           
            if (len + length > lWidth) {
                if(len == 0) {
                    outStr += strTemp.substring(0, length + len - lWidth); 
                    lWordVector.set(i, strTemp.substring(length + len - lWidth, length));
                }
                outStr = getMultiString(" ", lWidth - outStr.length() + (BeautifulPrint.SCREENWIDTH - lWidth - rWidth - mWidth)/2) + outStr;   
                lStrVector.add(outStr);
                outStr = "";               
                continue;              
            } else {               
                outStr += strTemp + " ";
                i++;               
                if(i == lWordVector.size()) {
                    outStr = getMultiString(" ", lWidth - outStr.length() + (BeautifulPrint.SCREENWIDTH - lWidth - rWidth - mWidth)/2) + outStr;
                    lStrVector.add(outStr);
                    outStr = "";
                    break;
                }
            }      
        }
       
        // process right word list
        i = 0;
        outStr = "";
       
        Vector rStrVector = new Vector();      
        while (true) {                 
            len = outStr.length();
           
            String strTemp = (String)rWordVector.get(i);
            length = strTemp.length();
           
            if (len + length > rWidth) {
                if(len == 0) {
                    outStr += strTemp.substring(0, length + len - rWidth); 
                    lWordVector.set(i, strTemp.substring(length + len - rWidth, length));
                }              
                rStrVector.add(outStr);
                outStr = "";
                continue;              
            } else {
                outStr += strTemp + " ";
                i++;               
                if(i == rWordVector.size()) {
                    rStrVector.add(outStr);
                    outStr = "";
                    break;
                }
            }      
        }
       
        // process the two strVector togother      
        int min = Math.min(lStrVector.size(), rStrVector.size());
        for (i = 0; i < min; i++) {
            outStr = (String)lStrVector.get(i) + getMultiString(" ", mWidth) + (String)rStrVector.get(i);
            println(outStr);
        }
        for (; i < lStrVector.size(); i++) {
            outStr = (String)lStrVector.get(i);
            println(outStr);
        }
        for(; i < rStrVector.size(); i++) {
            outStr = getMultiString(" ", lWidth + mWidth)+(String)rStrVector.get(i);
            println(outStr);
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        BeautifulPrint bp = new BeautifulPrint();
        String str = "Chinese Provinces and Citys List";
        bp.formatPrint(str, 25, BeautifulPrint.ALIGN_CENTER);
       
        String[] citys = {"Acheng", "Amoy [Xiamen]", "Anda", "Ankang",
                "Anqing", "Anshan", "Anshun", "Anyang", "Aqsu",
                "Baicheng", "Baiyin", "Baoding", "Baoji",
                "Baotou", "Beihai", "Beipiao", "Bei??an",
                "Bengbu", "Benxi", "Binzhou" };
        String[] provinces = {"Heilongjiang", "Fujian", "Heilongjiang",
                "Shaanxi", "Anhui", "Liaoning", "Guizhou", "Henan",
                "Xinxiang", "Jilin", "Gansu", "Hebei", "Shaanxi",
                "Inner Mongolia", "Guangxi", "Liaoning", "Heilongjiang",
                "Anhui", "Liaoning", "Shandong" };
        String cast = "The Cast";
        String order = "(in order of appearance)";
        String[] roles = {"Wladyslaw Sapilman", "Dorota", "Jurek", "Henryk", "Mother"};
        String[] actors = {"ADRIEN BRODY","EMILIA FOX","MICHAL ZEBROWSKI","ED STOPPARD","MAUREEN LIPMAN"};
       
        bp.formatPrint(cast, 25, BeautifulPrint.ALIGN_CENTER);
        bp.formatPrint(order, 25, BeautifulPrint.ALIGN_CENTER);
        for (int i = 0; i < roles.length; i++) {
            bp.advancedFormatPrint(roles[i], actors[i], 20, 20, 6);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值