Java 用另一个值替换字符串数组索引值

Java 用另一个值替换字符串数组索引值,java,Java,我有一个逗号分隔值的文件,我已将其读入我的程序,并将每一列(只有两列)的值放入其各自的字符串数组中。其中一列有三个无效(故意)的值,我想用“无效条目”之类的内容替换这些特定的数组索引值 下面的代码以我设置的格式打印无效的列和条目;但是,对于无效的值,程序只打印该值。我试图让程序用“无效条目”替换该值,但这似乎不起作用 public static String[] setWeightPercentageToOunce() { List<String> listStringBuf

我有一个逗号分隔值的文件,我已将其读入我的程序,并将每一列(只有两列)的值放入其各自的字符串数组中。其中一列有三个无效(故意)的值,我想用“无效条目”之类的内容替换这些特定的数组索引值

下面的代码以我设置的格式打印无效的列和条目;但是,对于无效的值,程序只打印该值。我试图让程序用“无效条目”替换该值,但这似乎不起作用

public static String[] setWeightPercentageToOunce() {
    List<String> listStringBuffer = new ArrayList<>(ouncesArray.length);
    String[] stringBuffer = new String[0];
    String[] stringReplaceBuffer = new String[0];
    String str;
    String str1;
    double moduloChecker;
    for (int i=0;i<ouncesArray.length;i++){
        double stringToDouble = Double.parseDouble(ouncesArray[i]);
        moduloChecker = stringToDouble % 625; // checks to verify the value is divisible by 625 with no remainder
        if (moduloChecker != 0) { // if there's a remainder, the value is invalid and should be replaced with "Invalid Entry".
            str = Double.toString(stringToDouble);
            listStringBuffer.add(str);
            stringBuffer = listStringBuffer.toArray(new String[0]);
            stringBuffer[i] = stringBuffer[i].replace(stringBuffer[i],"Invalid Entry!");
        } else {
            double conversion = stringToDouble / 625; // converts weight percentage to ounce and stores in double variable
            str = Double.toString(conversion);
            listStringBuffer.add(str);
        }
            stringBuffer = listStringBuffer.toArray(new String[0]);
    }
    return stringBuffer;
}
输出应为:

Lb 233 Oz 12.0
Lb 23 Oz 8.0
Lb 56 Oz 5.0
Lb 79 Oz 1.0
Lb 45 Oz 13.0
Lb 76 Oz 6.0
Lb 76 Oz 10.0
Lb 15 Oz 7.0
Lb 56 Oz 6.0
Lb 345 Oz Invalid Entry
Lb 34 Oz 7.0
Lb 654 Oz 11.0
Lb 8 Oz Invalid Entry
Lb 5 Oz 5.0
Lb 987 Oz 4.0
Lb 56 Oz 13.0
Lb 24 Oz 4.0
Lb 92 Oz 0.0
Lb 35 Oz 5.0
Lb 32 Oz 1.0
Lb 35 Oz Invalid Entry

抱歉,贾斯汀,但是我认为你粘贴的代码有一些问题和冗余。因此,根据您的要求,我将代码简化为以下内容。请检查这是否有效。基本上,这些变化是:

  • 为了便于理解,我声明了
    listStringBuffer
    一个数组,而不是一个列表。如果需要列表,则必须用列表替换
  • 我假设
    setWeightPercentageToOunce()
    为原始盎司数组中的每个盎司条目返回一个转换值数组
  • 代码如下:

    public static String[] setWeightPercentageToOunce() {
        String[] listStringBuffer = new String[ ouncesArray.length ];
        double moduloChecker;
        for (int i=0;i<ouncesArray.length;i++){
            double stringToDouble = Double.parseDouble(ouncesArray[i]);
            moduloChecker = stringToDouble % 625; // checks to verify the value is divisible by 625 with no remainder
            if (moduloChecker != 0) { // if there's a remainder, the value is invalid and should be replaced with "Invalid Entry".
                listStringBuffer[i] = "Invalid Entry!";
            } else {
                double conversion = stringToDouble / 625; // converts weight percentage to ounce and stores in double variable
                String str = Double.toString(conversion);
                listStringBuffer[i] = str;
            }
        }
        return listStringBuffer;
    }
    
    public静态字符串[]setWeightPercentageToOunce(){
    String[]listStringBuffer=新字符串[ouncesArray.length];
    双模切割器;
    
    对于(int i=0;i在
    if(moduleChecker!=0){
    之后打印,以检查是否注意到无效的出现。请添加一些输入文件/列表的示例行,以更容易地再现错误。您可以简化
    stringBuffer[i]=stringBuffer[i]。替换(stringBuffer[i],“无效条目!”;
    。它只是:
    stringBuffer[i] =“无效条目!”;
    public static String[] setWeightPercentageToOunce() {
        String[] listStringBuffer = new String[ ouncesArray.length ];
        double moduloChecker;
        for (int i=0;i<ouncesArray.length;i++){
            double stringToDouble = Double.parseDouble(ouncesArray[i]);
            moduloChecker = stringToDouble % 625; // checks to verify the value is divisible by 625 with no remainder
            if (moduloChecker != 0) { // if there's a remainder, the value is invalid and should be replaced with "Invalid Entry".
                listStringBuffer[i] = "Invalid Entry!";
            } else {
                double conversion = stringToDouble / 625; // converts weight percentage to ounce and stores in double variable
                String str = Double.toString(conversion);
                listStringBuffer[i] = str;
            }
        }
        return listStringBuffer;
    }