Split Error over IP String

4

Could someone tell me why this method with split() does not return the array correctly? Thanks

My intention is to send a String to the method that would be an ip and that the method returns that IP in an array of Strings.

public class hola {


    public String[] metoth2 (String a) {
        System.out.println("Ip obtenida");

        System.out.println(a);
        String ip[] = a.split(".");


         System.out.println("Method2Array");
        for (int i = 0; i < ip.length; i++) {
            System.out.println(ip[i]);
        }   


        return ip;


    }

Main Class:

public class Main {

    public static void main(String[] args) {
        Hola text = new Hola();
        String ipS="192.168.12.12";


        String ip[];

        ip=text.metoth2(ipS);

        System.out.println("Printamos ip");

        for (int i = 0; i < ip.length; i++) {
            System.out.print(ip[i]);
        }
    }

}
    
asked by Iron Man 19.12.2018 в 01:32
source

1 answer

6

In Java, if you use the method split () this function for regular expressions, therefore in the case of using the "." you must use:

.split("\.");

in the case of your code:

String ip[] = a.split("\.");

This says the documentation:

  

split () Divide this string around matches from the   regular expression given.

Unlike Javascript where if you can use .split(".") without problem.

Example:

Javascript problem number 2 and 3 figures

    
answered by 19.12.2018 / 01:45
source