这题主要有个思想直接体会:方向思维解决问题更方便
package com.pagination.plus.workTrain;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
* 长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
* 连续输入字符串(输入2次,每个字符串长度小于100)
* 输出到长度为8的新字符串数组
*/
public class Main {
public static void main(String[] args) throws IOException {
//BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\JavaData\\tmp/input.txt")));
//Scanner sc = new Scanner(System.in);
String s = null;
while ((s=bufferedReader.readLine())!=null) {
StringBuilder sb = new StringBuilder();
String str = s.trim();
for (int i=0;i<str.length();i++) {
if(' '!=str.charAt(i)){
sb.append(str.charAt(i));
}
}
//System.out.println(sb.toString());
//第一种复杂方法,正逻辑处理:考虑先分段在是否补0
/* if(sb.length()>0&&sb.length()<8){
int cout = 8-sb.length();
while (cout>0){
sb.append(0);
cout--;
}
System.out.println(sb.toString());
}else
if(sb.length()==8){
System.out.println(sb.toString());
}else{
int segmets = sb.length()/8;
int mod = sb.length()%8;
for(int i=0,j=0;i<segmets;i++,j+=8){
System.out.println(sb.substring(j,j+8));
}
if(mod!=0){
String substring = sb.substring(sb.length() - mod, sb.length());
StringBuilder stringBuilder = new StringBuilder(substring);
for(int i=0;i<8-mod;i++){
stringBuilder.append(0);
}
System.out.println(stringBuilder.toString());
}
}*/
//第二种简单方法,反向思维,首先判断是否需要补0,然后直接一个方法搞定
if(sb.length()%8!=0){
for(int i=0;i<8;i++){
sb.append(0);
}
}
for(int i=0,j=0;i<sb.length()/8;i++,j+=8){
System.out.println(sb.substring(j,j+8));
}
}
}
}