格式化 java 中的字符串以完美匹配
Formatting a string in java to match up perfectly
所以我想知道是否有人可以帮助我格式化这个...
这是目前的样子:
这就是我需要的样子(没有 ...):
这是我目前的代码:
System.out.format( "%n$------------------------------------$");
System.out.format( "%nWITHHOLDING FOR EACH EMPLOYEE%n%n" );
System.out.format( "First Name Last Name Hourly Rate Weekly Pay Withholding Amount%n%n" );
for( int i = 0; i < employeeNum; i++ ) {
System.out.format( "%-10s %-10s %-10.3f %-10.3f %-10.3f%n", employeeFirstName[i], employeeLastName[i],
employeeHourlyPay[i], weeklyPay[i], withholdingAmt[i] );
}
System.out.format( "%nEND OF REPORT%n" );
System.out.format( "$------------------------------------$");
感谢您的帮助!
使用它来截断长字符串:
static String truncto( String s, int max ){
return s.length() > max ? s.substring(0,max) : s;
}
您可以使用它来获得对齐的输出
System.out.format( "First Name Last Name Hourly Rate Weekly Pay Withholding Amount%n%n" );
System.out.format( "%-17s %-16s %-18.3f %-17.0f %-17.0f%n",
truncto("John-Bulwer-Lytton",17),
truncto("Smith",16),
123.45, 200.0, 100.0 );
使用您的数组元素代替我用于测试的文字。
您肯定可以为 header 打印两行。
这里我已经做到了,只是也使用header中的对齐:
System.out.print( "%n$------------------------------------$");
System.out.printf( "%nWITHHOLDING FOR EACH EMPLOYEE%n%n" );
System.out.printf("%-12s %-12s %-12s %-12s %-12s \n\n", "First Name", "Last Name", "Hourly Rate", "Weekly Pay", "Withholding Amount");
for( int i = 0; i < employeeNum; i++ ) {
System.out.printf( "%-12s %-12s %-12.3f %-12.3f %-12.3f \n", employeeFirstName[i], employeeLastName[i],
employeeHourlyPay[i], weeklyPay[i], withholdingAmt[i] );
}
System.out.printf( "%nEND OF REPORT%n" );
System.out.print( "$------------------------------------$");
这是输出:
...
First Name Last Name Hourly Rate Weekly Pay Withholding Amount
Brett Lawless 25.500 1020.000 204.000
....
所以我想知道是否有人可以帮助我格式化这个...
这是目前的样子:
这就是我需要的样子(没有 ...):
这是我目前的代码:
System.out.format( "%n$------------------------------------$");
System.out.format( "%nWITHHOLDING FOR EACH EMPLOYEE%n%n" );
System.out.format( "First Name Last Name Hourly Rate Weekly Pay Withholding Amount%n%n" );
for( int i = 0; i < employeeNum; i++ ) {
System.out.format( "%-10s %-10s %-10.3f %-10.3f %-10.3f%n", employeeFirstName[i], employeeLastName[i],
employeeHourlyPay[i], weeklyPay[i], withholdingAmt[i] );
}
System.out.format( "%nEND OF REPORT%n" );
System.out.format( "$------------------------------------$");
感谢您的帮助!
使用它来截断长字符串:
static String truncto( String s, int max ){
return s.length() > max ? s.substring(0,max) : s;
}
您可以使用它来获得对齐的输出
System.out.format( "First Name Last Name Hourly Rate Weekly Pay Withholding Amount%n%n" );
System.out.format( "%-17s %-16s %-18.3f %-17.0f %-17.0f%n",
truncto("John-Bulwer-Lytton",17),
truncto("Smith",16),
123.45, 200.0, 100.0 );
使用您的数组元素代替我用于测试的文字。
您肯定可以为 header 打印两行。
这里我已经做到了,只是也使用header中的对齐:
System.out.print( "%n$------------------------------------$");
System.out.printf( "%nWITHHOLDING FOR EACH EMPLOYEE%n%n" );
System.out.printf("%-12s %-12s %-12s %-12s %-12s \n\n", "First Name", "Last Name", "Hourly Rate", "Weekly Pay", "Withholding Amount");
for( int i = 0; i < employeeNum; i++ ) {
System.out.printf( "%-12s %-12s %-12.3f %-12.3f %-12.3f \n", employeeFirstName[i], employeeLastName[i],
employeeHourlyPay[i], weeklyPay[i], withholdingAmt[i] );
}
System.out.printf( "%nEND OF REPORT%n" );
System.out.print( "$------------------------------------$");
这是输出:
...
First Name Last Name Hourly Rate Weekly Pay Withholding Amount
Brett Lawless 25.500 1020.000 204.000
....