0

I'm trying to compile and test a code I saw online for Expanding an IP add. However when I try to compile it I am getting an error about the StringBuilder replace method. It says :

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)

I checked that the code's replace method, it shows the correct data type which is (int,int,String). I wonder how I can run this program.

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();
        }

}
Andrew
  • 75
  • 2
  • 7

4 Answers4

1

I think you are trying to replace between start character and end character. In that case you should get indexes of those characters and use them in replace method. Otherwise, you have to cast the characters to int to make them applicable in method. But I guess that's not what you are interested in.

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

You can simply use Stringbuilder#setCharAt to set character at specific location. But not sure whether it will fulfill your requirement or not.

As already stated in Jobin's answer, you are using incorrect method for String. You can use replace method of String instead of StringBuilder.

For example,

address = address.replace("::", ":");//Replace all :: with :
address = address.replaceFirst("::", ":");//Replace first occurrence of :: with :
akash
  • 22,664
  • 11
  • 59
  • 87
  • 1
    Sorry, Im not quite sure.. am I suppose to remove the if statement if(tempCompressLocation!=-1){ address.replace(tempCompressLocation,tempCompressLocation+2,":"); and replace it with your code? – Andrew Dec 05 '16 at 04:46
1

you are trying to use replace method from StringBuilder class on a String..

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

address is a String not a StringBuilder

Jobin
  • 5,610
  • 5
  • 38
  • 53
1

Try this:

StringBuilder address = new StringBuilder(sc.nextLine());
Arif Acar
  • 1,461
  • 2
  • 19
  • 33
  • I tried it, but it's giving me another error: error: incompatible types: unexpected return value return uncompressedAddress.toString(); – Andrew Dec 05 '16 at 04:56
  • Because you are returning something main method. Write it console it with this System.out.println(uncompressedAddress.toString()); instead of return uncompressedAddress.toString(); – Arif Acar Dec 05 '16 at 04:59
1

The String.replace method takes either two CharSequences or two chars. I think you can replace the string itself by using:

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

See if that works for your example. Good luck!

x80486
  • 6,627
  • 5
  • 52
  • 111
  • It's showing another error: IPadd.java:94: error: incompatible types: unexpected return value return uncompressedAddress.toString(); – Andrew Dec 05 '16 at 05:01
  • Yeah! I forgot about that one: you can't return from a `void` method. Either delete the last line: `return uncompressedAddress.toString()`, or change the `return` for a `System.out.print`...in case you want to see the `uncompressedAddress` value. – x80486 Dec 05 '16 at 05:05