使用substring截取指定长度的字符串,然后通过取模来计算最后剩余几个字符,用8减去剩余字符长度可得到需要补几个0。

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            int length = 8;
            int n = a.length()/length+1;
            String split[] = new String[n];
            for(int i=0;i<n;i++){
                if(i<n-1){
                    split[i] = a.substring(length*i,length*(i+1));
                }
                else{
                    split[i] = a.substring(length*i);
                    if(a.length()%length!=0){
                        for(int j=0;j<(8-(a.length()%length));j++){
                            split[i]=split[i]+"0";
                        }
                    }
                }
                System.out.println(split[i]);
            }
            
        }
    }
}

优化后的代码如下(先把0补齐到8的整数倍,然后再分割,调用分割次数减少,减少内存占用,提高运算速度):


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            int length = 8;
            int b = a.length();
            int n = (a.length()+length-1)/length;
            String split[] = new String[n];
            for(int i=0;i<n;i++){
                if(b%length!=0&&i==(n-1)){
                    for(int j=0;j<length-(b%length);j++)
                        a = a+"0";
                }
                
                split[i] = a.substring(length*i,length*(i+1));
                System.out.println(split[i]);
            }
            
        }
    }
}