Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Printf和regex格式化Java输出_Java_Regex - Fatal编程技术网

Printf和regex格式化Java输出

Printf和regex格式化Java输出,java,regex,Java,Regex,我需要使用printf来打印文本文件中的数据(HashMap映射已经填充,Account值不是null,一切正常)。但是如何使用正则表达式不仅保持列之间20个空格的均匀间距,而且将最后一列“Balance”四舍五入到小数点后1位?我看过类似的线程,但这些格式化规则要么抛出错误,要么根本没有效果。我尝试使用“%20s%20s%20s%20s%20s.1f\n”作为第一个参数,但结果是: ID First Name Last Na

我需要使用printf来打印文本文件中的数据(HashMap映射已经填充,Account值不是null,一切正常)。但是如何使用正则表达式不仅保持列之间20个空格的均匀间距,而且将最后一列“Balance”四舍五入到小数点后1位?我看过类似的线程,但这些格式化规则要么抛出错误,要么根本没有效果。我尝试使用“%20s%20s%20s%20s%20s.1f\n”作为第一个参数,但结果是:

              ID           First Name            Last Name              Balance.1f 
            107H          Alessandro                Good             6058.14
            110K               Kolby                Cain              8628.4
            100A              Matias                Kane              290.99
            103D               Macie               House              631.12
            108I               Allan              Turner              914.89
            106G               Nancy               Avery             5201.38
            105F               Semaj               Olsen              344.63
            109J              Wilson              Hudson              771.65
            102C               Alana              Farmer              2004.5
            101B           Johnathan             Burgess              457.35
            104E              Andres              Rivers             3487.87
所有账户的总余额为:28790.92美元

所以它显然只对几个数据点起作用

这是我的密码:

public void printData() {
        System.out.printf("%20s %20s %20s %20s%.1f \n", "ID", "First Name", "Last Name", "Balance");
        for(Account a : map.keySet()) {
            // could do a.getID(), but rather get value from ArrayList
            ArrayList<String> myList = map.get(a); // retrieve the VALUE (ArrayList) using the key (Account a)
            for(String s : myList) {
                System.out.printf("%20s", s);
            }
            System.out.println();
        }
    }
public void printData(){
System.out.printf(“%20s%20s%20s%20s%20s%.1f\n”、“ID”、“名字”、“姓氏”、“余额”);
对于(帐户a:map.keySet()){
//可以执行.getID(),但可以从ArrayList获取值
ArrayList myList=map.get(a);//使用键(帐户a)检索值(ArrayList)
for(字符串s:myList){
System.out.printf(“%20s”,s);
}
System.out.println();
}
}

在您的代码中,“balance”列下每行的小数仅作为字符串打印。第一行中的
%.1f
仅用于标题行,作为未接收的第五个参数的格式,并且未应用于下面的每一行。 试试这个:

System.out.printf("%20s %20s %20s %20s\n", "ID", "First Name", "Last Name", "Balance");
for(Account a : map.keySet()) {
    ArrayList<String> myList = map.get(a);

    //print the first 3 columns as strings
    for(int i = 0; i < myList.size()-1; i++) {
        String s = myList.get(i);
        System.out.printf("%20s", s);
    }

    //print the last column as a decimal rounded to 1 place after the decimal
    float balance = Float.parseFloat(myList.get(myList.size()-1));
    System.out.printf("%20s%.1f", "", balance);

    System.out.println();
}
System.out.printf(“%20s%20s%20s%20s%20s\n”、“ID”、“名字”、“姓氏”、“余额”);
对于(帐户a:map.keySet()){
ArrayList myList=map.get(a);
//将前3列打印为字符串
对于(int i=0;i
您的输出应该是什么?您的意外输出是清楚的。