比如:每一项规定有10位的长度。实际上填充的内容有可能有4位,7位或者12位,怎样写一段代码来实现呢〉?谢谢

解决方案 »

  1.   

    比如说:一段java代码去读一个excel表,excel表里含有:姓名,籍贯,家庭住址等内容,这些内容的长度是不同的,现在要想输出的格式看起来整齐,就预先规定每项内容占16位,不足16位的后面填空格。请问该怎么样实现呢?
      

  2.   

    你在写excel 时让起内容左对齐 或者 右对齐 不就整齐了 不用填充吧,excel定义好大小是不变的呀
      

  3.   

    我是读取excel表格,用文档形式显示出来。
      

  4.   

       1  张毛毛    黑龙江省哈尔滨市XX路123号610室     123.50
      22  张毛      哈尔滨市XX路123号610室            1123.50
     333  张毛毛毛  XX路123号610室                   34123.50import java.text.DecimalFormat;public class Test1 {
        public static void main(String[] args) {
            DataBean[] beans = new DataBean[]{
                    new DataBean(1, "张毛毛", "黑龙江省哈尔滨市XX路123号610室", 123.50f),
                    new DataBean(22, "张毛", "哈尔滨市XX路123号610室", 1123.50f),
                    new DataBean(333, "张毛毛毛", "XX路123号610室", 34123.50f)
                };
            Format format = new Format();
            for(int i = 0; i < beans.length; i++) {
                DataBean bean = beans[i];
                format.print(bean.getId(), 4, 2, false);
                format.print(bean.getName(), 8, 2, true);
                format.print(bean.getAddress(), 30, 1, true);
                format.print(bean.getMoney(), 10, 0, false);
                System.out.println();
            }
        }    
    }class Format {
        private int getLen(String str) {
            char[] cs = str.toCharArray();
            int len = 0;
            for(int i = 0; i < cs.length; i++) {
                if(cs[i] > 0xff) {
                    len += 2;
                }else{
                    len++;
                }
            }
            return len;
        }
        
        private String toPerttyString(String str, int width, int separatorWidth, boolean isLeft) {
            int len = getLen(str);
            String space = getSpace(width - len);
            if(isLeft) {
                return str + space + getSpace(separatorWidth);
            } else {
                return space + str + getSpace(separatorWidth);
            }
        }
        
        private String getSpace(int width) {
            if(width < 1) {
                return "";
            }
            char[] c = new char[width];
            for(int i = 0; i < width; i++) {
                c[i] = ' ';
            }
            return new String(c);
        }
        
        public void print(String str, int width, int separatorWidth, boolean isLeft) {
            System.out.print(toPerttyString(str, width, separatorWidth, isLeft));
        }
        public void print(int num, int width, int separatorWidth, boolean isLeft) {        
            System.out.print(toPerttyString(num + "", width, separatorWidth, isLeft));
        }
        public void print(double num, int width, int separatorWidth, boolean isLeft) {
            DecimalFormat format = new DecimalFormat(".00");
            System.out.print(toPerttyString(format.format(num), width, separatorWidth, isLeft));
        }
    }class DataBean {
        private int id;
        private String name;
        private String address;
        private float money;
            
        public DataBean(int id, String name, String address, float money) {
            this.id = id;
            this.name = name;
            this.address = address;
            this.money = money;
        }
        
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public float getMoney() {
            return money;
        }
        public void setMoney(float money) {
            this.money = money;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
      

  5.   

    str += "                ";
    System.out.println(str.substring(0,16));
    这样行不?
      

  6.   

    谢谢大家了,我刚学Java,以后还请大家多多指教小弟^0^
      

  7.   

    读文件的每一行。把每一行的数据构造成一个数组,每一个单元格的数据为该数组的一个元素。
    楼主的任务就是数组配对的问题了,一个if判断加上一个for循环,以及一个中间变量就搞定!
    1.确定最大长度maxLen。
    2.比较每一行的实际长度lineLen;如果lineLen < maxLen ,配对。使用for循环,循环起点为 lineLen 循环终点为 maxLen,在循环内 对长度不够,用空格补足。lineLen >= maxLen 的情况不存在。