3

I have this code which takes an IP address(string) of the format 255.255.255.255 and needs to perform some post processing on those numbers (not posted here) but for which the string must be converted into an array of ints.

I have used here the split() method but it's not giving me the result. I saw other answers on sp doing it with regex but none of them worked for me.

import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        String text;

        Scanner take=new Scanner(System.in);
        text=take.nextLine();
        String data[]=text.split(".",4);

        for(String w:data){
            System.out.println(w);
        }
        take.close();
    }
}

I have tried with the input 12.36.26.25

But it outputs 36.26.25 which was supposed to be like 12 36 26 25

5
  • i love the "please dont mark as duplicate" haha. gimme a sec to look over the code Commented Feb 12, 2019 at 8:35
  • 3
    Try escaping the period (\\.) as it's a special regex symbol (it matches any character). Commented Feb 12, 2019 at 8:35
  • can u just tell me where to put the modification? Commented Feb 12, 2019 at 8:36
  • . is regex match. Use escape characters. Commented Feb 12, 2019 at 8:38
  • if you want a challange, write your own "split" method Commented Feb 12, 2019 at 8:40

5 Answers 5

11

Use it like that:

        String example="12.36.26.25";
        String data[]=example.split("\\.");

        for(String w:data){
            System.out.println(w);
        }

And it will do what you want ;)

Using split(regex,limit) as you did will actually split on any character (since . is the regex for any character) and it will basically remove the first few characters

Sign up to request clarification or add additional context in comments.

5 Comments

as simple as that lmao
what is the significance of " \\." can u please explain?
let me refer to this post aswell stackoverflow.com/questions/3674930/… in conjunction to Veselin Davidov's answer
It escapes the '.' character. Basically '.' is the regex form of any character
If you're not familiar with regular expressions, check out stackoverflow.com/questions/2912894/… .. or just do some googling ;)
2

Solution using java.util.regex package:

import java.util.regex.*;
public class Main{

     public static void main(String args[]){
        String text = "12.36.26.25";
        String separator = ".";
        String[] data = text.split(Pattern.quote(separator));
        System.out.println(data.length);
        for(String w: data){
            System.out.println(w);
        }
    }
}

The Pattern.quote will do the escaping.

Comments

0

Let me give you guys an alternative solution aswell:

public static void main(String args[]){
        String text;

        Scanner take=new Scanner(System.in);
        text=take.nextLine();
        String data[]=text.split("[.]");

        for(String w:data){
            System.out.println(w);
        } 
        take.close();
    }

you can escape the dot using "[.]" as a string, turning the dot from an regex operator into a literal char

Comments

0

Just change this line

String data[]=text.split(".",4);

to

String data[]=text.split("\\.");

1 Comment

For all the special character the escape \\ is required. They are \, ^, $, ., |, ?, *, +, (, ), [, ], {, }
0

An alternate solution is to use the sun.net.util.IPAddressUtil to extract the numbers from a given String of an IPAddress as below-

String ipAddressString = "10.20.30.40";
byte[] numbers = IPAddressUtil.textToNumericFormatV4(ipAddressString);

The numbers in ByteArray format can be converted to integer array using this solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.