将字符串填充到任意长度的方法

时间:2014-04-09 08:35:17

标签: java arrays string padding

我有一个问题,想弄清楚如何填充从我的数组值打印的字符串。我认为我有一个很好的方法可以做到这一点,但是它没有正确读取x.length所以我很好奇其他方法甚至是对bug的解释。

import java.util.Scanner;
//================================================================
public class Array {
//----------------------------------------------------------------
    private static Scanner Keyboard = new Scanner(System.in);

    public static void main(String[] args) {
    //----------------------------------------------------------------
        char group, rLetter,letter;
        String choice ;
        String str = " ";
        double sum     =  0;
        int num     = 10; // for test
        int rows    = 10;
        int columns =  8;
        double average = 0;
        int S;
        int diff = 0;
        int totalChar=0;
        int minLen = 20;
        String x= "";

        // creating 2d array


        System.out.print("Please enter number of rows               : ");
        rows = Keyboard.nextInt();
        Keyboard.nextLine();


        double[][] figures = new double[rows][num];

        for(int t = 0; t < rows; t++) { 
            rLetter = (char)((t)+'A');
            System.out.print("Please enter number of positions in row " + rLetter + " : "); 
                columns = Keyboard.nextInt(); 
                Keyboard.nextLine(); 

            figures[t] = new double[columns]; 
        }

        // filling the array
        for(int row = 0; row < figures.length; ++row) {
            for(int col = 0; col < figures[row].length; ++col) {
                figures[row][col] = 0.0;
            }
        }

        // printing the array
        for(int row=0; row<figures.length; ++row) {
            // printing data row
            group = (char)((row)+(int)'A');
            System.out.print(group+" : ");
            for(int col=0; col<figures[row].length; ++col) {
                sum += figures[row][col];
                average = sum/figures[row].length;

                x = "   "+figures[row][col];
                diff = minLen - x.length();
                System.out.printf("%1$" + diff + "s", x);

                System.out.print(" ");

            }
            System.out.print("["+average+"]");
            System.out.println();


}
}
}

我想生成一个如下所示的表:

A:   0.0   0.0   0.0                        -        [    0.0,     0.0]

B:   0.0   0.0   0.0   0.0                    -      [    0.0,     0.0]

C:   0.0   0.0   0.0   0.0   0.0           -         [    0.0,     0.0]

在哪里 - 我想要填充空格直到任意长度。换句话说,我希望我的打印数组部分能够打印每一行并添加空格到一个点。有可能为此制定方法吗?我还不熟悉所有的java方法,所以任何帮助都表示赞赏。 如果这是一个重复的问题,我将很感激重定向到原文,我会尝试删除这个。同时也很抱歉在这种情况下给您带来不便。

2 个答案:

答案 0 :(得分:1)

我假设您知道如何使用StringBuilder或字符串连接在左侧和右侧构建字符串。

构建完字符串后,可以使用下面的printf将右边的输出填充到所需数量的空白字符。在下面的示例代码中,我使用20作为要填充的字符数。

public class Pad {
    public static void main(String[] args) {
        String a = "0.0 0.0 0.0 ";
        String b = "0.0 0.0 0.0 0.0";
        String c = "[ 0.0, 0.0]";
        System.out.printf("%-20s %s\n", a, c);
        System.out.printf("%-20s %s\n", b, c);
    }
}

您可以在上面的代码中使用这个想法,将上一个循环替换为:

    // printing the array
    for(int row=0; row<figures.length; ++row) {
        // printing data row
        StringBuilder sb = new StringBuilder();

        group = (char)((row)+(int)'A');
        sb.append(group+" : ");
        for(int col=0; col<figures[row].length; ++col) {
            sum += figures[row][col];
            average = sum/figures[row].length;

            x = "   "+figures[row][col];
            diff = minLen - x.length();
            sb.append(String.format("%1$" + diff + "s", x));

            sb.append(" ");

        }
        System.out.printf("%-75s[%f]\n", sb.toString(), average);
    }

答案 1 :(得分:0)

您需要找到最大列数并在每行中迭代到最大列,如果长度小于实际行长度则放置值,否则放置空格。 它将解决问题。

我添加了更改后的代码:

public class Array {

    // String result = String.format("The format method is           %s!", "great");
    //  System.out.println(result);
    //----------------------------------------------------------------
    private static Scanner Keyboard = new Scanner(System.in);

    public static void main(String[] args) {
        //----------------------------------------------------------------
        char group, rLetter, letter;
        String choice;
        String str = " ";
        double sum = 0;
        int num = 10; // for test
        int rows = 10;
        int columns = 8;
        double average = 0;
        int S;
        int diff = 0;
        int totalChar = 0;
        int minLen = 20;
        String x = "";

        // creating 2d array


        System.out.print("Please enter number of rows               : ");
        rows = Keyboard.nextInt();
        Keyboard.nextLine();


        double[][] figures = new double[rows][num];
        int maxSize = 0;
        for (int t = 0; t < rows; t++) {
            rLetter = (char) ((t) + 'A');
            System.out.print("Please enter number of positions in row " + rLetter + " : ");
            columns = Keyboard.nextInt();
            Keyboard.nextLine();
            if (columns > maxSize) {
                maxSize = columns;
            }
            figures[t] = new double[columns];
        }

        // filling the array
        for (int row = 0; row < figures.length; ++row) {
            for (int col = 0; col < figures[row].length; ++col) {
                figures[row][col] = 0.0;
            }
        }

        // printing the array
        for (int row = 0; row < figures.length; ++row) {
            // printing data row



            group = (char) ((row) + (int) 'A');
            System.out.print(group + " : ");
            for (int col = 0; col < maxSize; ++col) {
                if (col < figures[row].length) {
                    sum += figures[row][col];
                    average = sum / figures[row].length;

                    x = "   " + figures[row][col];
                    diff = minLen - x.length();
                    System.out.printf("%1$" + diff + "s", x);

                    System.out.print(" ");
                } else {
                    System.out.print("               ");
                }
            }

            System.out.print("[" + average + "]");
            System.out.println();


        }
    }
}