急求:如何实现 每一项的实际长度不够额定长度就用空格填充 比如:每一项规定有10位的长度。实际上填充的内容有可能有4位,7位或者12位,怎样写一段代码来实现呢〉?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 比如说:一段java代码去读一个excel表,excel表里含有:姓名,籍贯,家庭住址等内容,这些内容的长度是不同的,现在要想输出的格式看起来整齐,就预先规定每项内容占16位,不足16位的后面填空格。请问该怎么样实现呢? 你在写excel 时让起内容左对齐 或者 右对齐 不就整齐了 不用填充吧,excel定义好大小是不变的呀 我是读取excel表格,用文档形式显示出来。 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; }} str += " ";System.out.println(str.substring(0,16));这样行不? 谢谢大家了,我刚学Java,以后还请大家多多指教小弟^0^ 读文件的每一行。把每一行的数据构造成一个数组,每一个单元格的数据为该数组的一个元素。楼主的任务就是数组配对的问题了,一个if判断加上一个for循环,以及一个中间变量就搞定!1.确定最大长度maxLen。2.比较每一行的实际长度lineLen;如果lineLen < maxLen ,配对。使用for循环,循环起点为 lineLen 循环终点为 maxLen,在循环内 对长度不够,用空格补足。lineLen >= maxLen 的情况不存在。 总结一下String iReport同时嵌套子报表和饼状图时出现多种错误 求助:关于getAppletContext()含义和用途 mysql连接问题 如何对JavaBean的某两个属性排序? JTable中如何对不同行的单元格的值进行判断 请教:急用URL.openStream()的时候,如果URL用主机名就抛出异常,用IP就没事 ★★请高手讲解catch、finally、thows ioexecption的区别? 有兴趣用java做java编辑器的进来谈谈心 一个很简单JBuilder问题 程序设计问题 谁能跟我细讲讲这3种同步的区别
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;
}
}
System.out.println(str.substring(0,16));
这样行不?
楼主的任务就是数组配对的问题了,一个if判断加上一个for循环,以及一个中间变量就搞定!
1.确定最大长度maxLen。
2.比较每一行的实际长度lineLen;如果lineLen < maxLen ,配对。使用for循环,循环起点为 lineLen 循环终点为 maxLen,在循环内 对长度不够,用空格补足。lineLen >= maxLen 的情况不存在。