【问题标题】:Java - StringBuilder replace() shows error: method String.replace(char,char) is not applicableJava - StringBuilder replace() 显示错误:方法 String.replace(char,char) 不适用
【发布时间】:2016-12-05 04:30:42
【问题描述】:

我正在尝试编译和测试我在网上看到的Expanding an IP add 的代码。但是,当我尝试编译它时,我收到有关 StringBuilder 替换方法的错误。它说:

IPadd.java:52: error: no suitable method found for replace(int,int,String)
                    address.replace(tempCompressLocation,tempCompressLocation+2,":");
                           ^
method String.replace(char,char) is not applicable
  (actual and formal argument lists differ in length)

我检查了代码的替换方法,它显示了正确的数据类型,即 (int,int,String)。我想知道如何运行这个程序。

import java.util.*;

public class IPadd {

    public static void main (String[] args){

        Scanner sc = new Scanner(System.in);

        System.out.print("Input address: ");
        String address = sc.nextLine();

        // Store the location where you need add zeroes that were removed during uncompression

        int tempCompressLocation=address.indexOf("::");

        //if address was compressed and zeroes were removed, remove that marker i.e "::"
        if(tempCompressLocation!=-1){
            address.replace(tempCompressLocation,tempCompressLocation+2,":");
        }

        //extract rest of the components by splitting them using ":"
        String[] addressComponents=address.toString().split(":");

        for(int i=0;i<addressComponents.length;i++){
            StringBuilder uncompressedComponent=new StringBuilder("");
            for(int j=0;j<4-addressComponents[i].length();j++){

                //add a padding of the ignored zeroes during compression if required
                uncompressedComponent.append("0");

            }
            uncompressedComponent.append(addressComponents[i]);

            //replace the compressed component with the uncompressed one
            addressComponents[i]=uncompressedComponent.toString();
        }


        //Iterate over the uncompressed address components to add the ignored "0000" components depending on position of "::"
        ArrayList<String> uncompressedAddressComponents=new ArrayList<String>();

        for(int i=0;i<addressComponents.length;i++){
            if(i==tempCompressLocation/4){
                for(int j=0;j<8-addressComponents.length;j++){
                    uncompressedAddressComponents.add("0000");
                }
            }
            uncompressedAddressComponents.add(addressComponents[i]);

        }

        //iterate over the uncomopressed components to append and produce a full address
        StringBuilder uncompressedAddress=new StringBuilder("");
        Iterator it=uncompressedAddressComponents.iterator();
        while (it.hasNext()) {
            uncompressedAddress.append(it.next().toString());
            uncompressedAddress.append(":");
        }
        uncompressedAddress.replace(uncompressedAddress.length()-1, uncompressedAddress.length(), "");
        return uncompressedAddress.toString();
        }

}

【问题讨论】:

  • 你检查过什么代码的replace()方法? String.中没有这种方法

标签: java replace stringbuilder


【解决方案1】:

试试这个:

StringBuilder address = new StringBuilder(sc.nextLine());

【讨论】:

  • 我试过了,但它给了我另一个错误:错误:不兼容的类型:意外的返回值返回 uncompressedAddress.toString();
  • 因为您正在返回一些主要方法。用这个 System.out.println(uncompressedAddress.toString()); 编写控制台它而不是返回 uncompressedAddress.toString();
【解决方案2】:

您正在尝试在String 上使用StringBuilder 类中的替换方法..

address.replace(tempCompressLocation, tempCompressLocation + 2, ":");

地址是String 不是StringBuilder

【讨论】:

    【解决方案3】:

    String.replace 方法采用两个 CharSequences 或两个 chars。我认为您可以使用以下方法替换字符串本身:

    if (tempCompressLocation != -1) {
      address.replace("::", ":");
    }
    

    看看这是否适用于您的示例。祝你好运!

    【讨论】:

    • 它显示另一个错误:IPadd.java:94:错误:不兼容的类型:意外的返回值返回 uncompressedAddress.toString();
    • 是的!我忘了那个:你不能从void 方法返回。要么删除最后一行:return uncompressedAddress.toString(),要么将return 更改为System.out.print...以防您想查看uncompressedAddress 值。
    【解决方案4】:

    我认为您正在尝试在开始字符和结束字符之间进行替换。在这种情况下,您应该获取这些字符的索引并在替换方法中使用它们。否则,您必须将字符转换为 int 以使其适用于方法。但我想这不是你感兴趣的。

    int index = stringbuilder.indexOf(String.valueOf(tempCompressLocation));
    stringbuilder.replace(index, index + 2, ":");
    

    您可以简单地使用Stringbuilder#setCharAt 在特定位置设置字符。但不确定它是否能满足您的要求。

    正如 Jobin 的 回答中所述,您对字符串使用了不正确的方法。您可以使用 String 的 replace 方法而不是 StringBuilder。

    例如,

    address = address.replace("::", ":");//Replace all :: with :
    address = address.replaceFirst("::", ":");//Replace first occurrence of :: with :
    

    【讨论】:

    • 对不起,我不太确定.. 我想删除 if 语句 if(tempCompressLocation!=-1){ address.replace(tempCompressLocation,tempCompressLocation+2,":");并用您的代码替换它?
    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 2016-01-20
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多