Java 使用replaceAll()时输出错误

Java 使用replaceAll()时输出错误,java,Java,我有一个csv文件,其格式如下: 1,,3,4 3,4,,0 2,,4,2 我正在将该文件读入一个名为text的字符串变量。那么,这样做, String newText = text.replaceAll("","0"); 在我的输出中,它将无值元素替换为0,这正是我想要的,但它还在有值的元素之前和之后附加0 010 0 030 040 030 040 0 000 020 0 040 020 感谢您的帮助 编辑:这是我的Clarification代码的其余部分 import java.la

我有一个csv文件,其格式如下:

1,,3,4
3,4,,0
2,,4,2
我正在将该文件读入一个名为text的字符串变量。那么,这样做,

String newText = text.replaceAll("","0");
在我的输出中,它将无值元素替换为0,这正是我想要的,但它还在有值的元素之前和之后附加0

010
0
030
040
030
040
0
000
020
0
040
020
感谢您的帮助

编辑:这是我的Clarification代码的其余部分

import java.lang.*;

public class DataMatrix {

    private double[][] dMatrix; 

    public DataMatrix(String text) {

        String[] line = text.split("\n");
        // Creates an array for the CSV file
        String[][] sMatrix = new String[line.length][];

        for(int i=0; i < line.length; i++)
            sMatrix[i] = line[i].split(",");

        /*for(int j=0; j < sMatrix.length; j++) {
            for(int x=0; x <= line.length; x++) {
                if(sMatrix[j][x] !=*/ 

        System.out.println("Original String: ");

        for(int x=0; x < sMatrix.length; x++) {
            for(int j=0; j <= line.length; j++) {
                System.out.println(sMatrix[x][j]);
            }
        }

        dMatrix = new double[sMatrix.length][];
        System.out.println("Converted to double: ");

        for(int x=0; x < sMatrix.length; x++) {
            dMatrix[x] = new double[sMatrix[x].length];
            for(int j=0; j <= sMatrix.length; j++) {

                dMatrix[x][j] = Double.parseDouble(sMatrix[x][j]);
                System.out.println(dMatrix[x][j]);
            }
        }
    }

    public void avgRowValue() {

        double tempTotal = 0;

        for(int x=0; x < dMatrix.length; x++) {
            System.out.println("***********************************");
            System.out.println("The average for row "+(x+1)+" is: ");

            for(int i=0; i < dMatrix[x].length;i++) {
                tempTotal += dMatrix[x][i];
            double rowAvg = tempTotal / dMatrix[x].length;
            System.out.println(rowAvg);
            tempTotal = 0;
            }
        }

    }

    public void avgColValue() {

        double tempTotal = 0;

        for(int x=0; x < dMatrix[0].length; x++) {
            System.out.println("**************************************");
            System.out.println("The average for column "+(x+1)+" is: ");

            for(int i=0; i <= dMatrix.length-1; i++)
                tempTotal += dMatrix[i][x];
            double colAvg = tempTotal / dMatrix.length;
            System.out.println(colAvg);
            tempTotal = 0;
        }
    }

    public void overallAvgValue() {

        double tempTotal = 0;
        int count = 0;

        for(int x=0; x < dMatrix.length; x++) {
            for(int i=0; i <= dMatrix.length; i++) {
                tempTotal += dMatrix[x][i];
                count++;
            }
        }
        double overallAvg = tempTotal / count;
        System.out.println("The overall average for the data matrix is: "+overallAvg);
    }

    public void maxValue() {

        double maxValue = 0;

        for(int x=0; x < dMatrix.length; x++) {
            for(int i=0; i <= dMatrix.length; i++) {
                if(dMatrix[x][i] >= maxValue)
                    maxValue = dMatrix[x][i];
            }
        }
        System.out.println("The max value for the data matrix is: "+maxValue);
    }

    public void minValue() {

        double minValue = dMatrix[0][0];

        for(int x=0; x < dMatrix.length; x++) {
            for(int i=0; i <= dMatrix.length; i++) {
                if(dMatrix[x][i] <= minValue)
                    minValue = dMatrix[x][i];
            }
        }
        System.out.println("The min value for the data matrix is: "+minValue);
    }
}

我建议使用库(例如)读取该文件,然后在遇到空单元格时替换它们。它将更加健壮。

我知道,在您基于逗号拆分行之后,文本包含单个列的值。您想将空字符串更改为0

replaceAll方法采用正则表达式。对于正则表达式来说,空字符串很奇怪。您需要的是text.replaceAll^$,0。

使用text.replaceAll,0可以查找字符串中长度为零的子字符串的所有位置。对于任何字符串,这是介于任意两个字符之间以及字符串的开头和结尾

"12345".replaceAll("", "_")  // _1_2_3_4_5_
如果您真的想使用正则表达式,可以使用replaceAll^$,0,其中^和$分别是字符串的开头和结尾。或者,只需检查字符串是否为空:

String newText = text.isEmpty() ? "0" : text;
例如:

for (String s : "1,,23,4".split(",")) {
    System.out.println(s.isEmpty() ? "0" : s);  // prints 1, 0, 23, and 4
    System.out.println(s.replaceAll("^$", "0"));// prints 1, 0, 23, and 4
}

如果要将这些0插入原始字符串中,即在使用拆分之前,可以使用不同正则表达式的析取作为字符串开头、字符串中间和字符串结尾的大小写,使用断言,例如?我更喜欢在读取csv文件时这样做,其成本比读取csv文件后要低得多,所以我制作了一个csvreader和一个名为Line的数组,从csv中逐行读取,如果字符串的长度大于0,则以这种方式写入:0thatstring0,否则写入为0


第二种方法,我不提供它,是使用split方法并拆分每个字符串,使用replace方法,它就会工作

@azurefrog错误代码…@azurefrog错误示例,输出必须是:0 0,但对于混淆@azurefrog,您的代码输出没有任何问题!我要做的是读取csv文件,并将其放入我已经完成的数组中。然后,当csv文件中的一个点没有值时,我需要为它指定一个0。您已经从csv文件中读取了吗?现在你有一个字符串中的每一行吗?如果这是真的,那么我将写出真的answer@Mohsen_Fatemi不,OP不需要检查,,。他已经将该行拆分为不同的值。逗号已经不存在了。@KlitosKyriacou谢谢您的回复。我尝试了您所说的,但它并没有在我的输出中将空值替换为0。@NickM我看到您在编辑中粘贴的代码中所做的操作。replaceAll必须位于每个字段上。相反,您试图将其应用于整个文本!你应该在分手后打电话给replaceAll。如果您希望将其应用于整个文本,则需要调用replaceAll、、0、。@KlitosKyriacou-Awesome,这就解决了它!我现在只学了大概2个月的Java,所以我需要学习一些技术细节。谢谢你指出:谢谢你的回答。当我尝试replaceAll选项或text.isEmpty选项时,我收到一个数字格式异常错误,说明字符串为空。这是错误的,如果我输入123,,5,您的答案将是0102030 0 050@Mohsen_Fatemi怎么回事?您知道第一行代码不是我的答案,而是为了说明OP当前的代码在做什么,对吗?下面的两种方法中的任何一种会如何产生输出?现在没问题了:我喜欢你的3 replaceAll方法,它很适合使用CSVReader而不是split。但您似乎误解了这些要求。OP不希望123,5变成01230,0050。他想要123,0,5。如果需要,只需打印第[i]行即可!第[i]行。我是空的。
for (String s : "1,,23,4".split(",")) {
    System.out.println(s.isEmpty() ? "0" : s);  // prints 1, 0, 23, and 4
    System.out.println(s.replaceAll("^$", "0"));// prints 1, 0, 23, and 4
}
String nums = ",1,,23,4,";
System.out.println(nums.replaceAll("(?<=^|,)(?=,|$)", "0"));
// output: 0,1,0,23,4,0
System.out.println(nums.replaceAll("(^|,)(,|$)", "$10$2"));
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] Line;
while ((Line = reader.readNext()) != null) {
    for(int i = 0 ; i < Line.length ; i++)
        if(Line[i].length>0) System.out.println(Line[i]);
        else System.out.println("0");
}