String.split() java function help

String s=“0.123”;
String x[]=s.split(".");
out.printLine(x[0]+" "+x[1]);

This is giving an ArrayIndexOutOfBounds Exception. Can someone please help???

Your code has several compiler errors - please post your full, formatted code!

Note that String.split takes a regex, so you probably meant something like this:


class Main{
	public static void main(String[] args) throws IOException{
        String s="0.123";
        String x[]=s.split("\\.");
        System.out.println(x[0]+" "+x[1]);

I just skipped the main function to make it short but thanks. What does a regex mean??
Like the split function does work for .split(" “) and .split(”,") without any exception.

1 Like

Why does the function work just fine with .split(" “) and .split(”,") I mean it gives the correct output with “,” instead of “\,”

, and “space”

have no special meaning in regexes;


does, so needs to be escaped.


Thanks for your help. I spent a lot of time before this on the net to find the mistake but couldn’t find one. Thanks once again.

1 Like