sách gpt4 ai đã đi

Java - StringBuilder replace() hiển thị lỗi: phương thức String.replace(char, char) không áp dụng được

In lại 作者:行者123 更新时间:2023-12-01 18:00:56 28 4
mua khóa gpt4 Nike

我正在尝试编译和测试我在网上看到的代码 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<>
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 uncompressedAddressComponents=new ArrayList();

for(int i=0;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();
}

}

1 Câu trả lời

Hãy thử cách này:

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

关于Java - StringBuilder 替换()显示错误 : method String. 替换(char,char)不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40967133/

28 4 0
Bài viết được đề xuất: iphone - if/else语句iPhone中的问题
Bài viết được đề xuất: java - JPanel 位于另一个 JPanel 之上
Bài viết được đề xuất: java - Java.util.Map接口(interface)的父类是哪个?
Bài viết được đề xuất: java - 如何删除无效的 XML 元素
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com