私はExpanding an IP addのオンラインで見たコードをコンパイルしてテストしようとしています。しかし、私がそれをコンパイルしようとすると、StringBuilderの置き換えメソッドに関するエラーが発生します。それは言う:Java - StringBuilder replace()はエラーを示します。メソッドString.replace(char、char)は適用されません
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.'にありません。 – EJP