Can Anyone Explain this code for me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
14th Oct 2021, 6:15 AM
Adithya Keshav T
Adithya Keshav T - avatar
13 Answers
+ 8
Adithya Keshav T Check this out: https://stackoverflow.com/questions/7935858/the-split-method-in-java-does-not-work-on-a-dot It seems like split method takes regular expressions as input not string and "." has a special meaning in regular expressions, so use ("\\."). Split can't convert the given expression, so it is returning nothing. That's why the length is 0. Source says: "As with any regular expression special character, you escape with a \. You need an additional \ for the Java string escape"
14th Oct 2021, 6:56 AM
Natu
Natu - avatar
+ 4
"." delimiter is each char each delimiter is ignored between chars there is nothing result is empty array this can get sense as String a[]=("76.88").split("\\.");
14th Oct 2021, 6:42 AM
zemiak
+ 3
Adithya Keshav T They are basically a search pattern, used to search a specific part from a String. More info: https://www.geeksforgeeks.org/regular-expressions-in-java/amp/
14th Oct 2021, 7:14 AM
Natu
Natu - avatar
+ 2
You can find the information here: https://stackoverflow.com/questions/7517704/java-split-returns-ljava-lang-string186d4c1-why It's a valid array of String, but if you want to print them you have to use a loop https://code.sololearn.com/cqQnEYKXMVk5/?ref=app
14th Oct 2021, 6:35 AM
Natu
Natu - avatar
+ 2
Natu the length of array should be 2 but it says 0 for my code
14th Oct 2021, 6:42 AM
Adithya Keshav T
Adithya Keshav T - avatar
+ 2
Thanks zemiak split("\\.") is giving the output i expected
14th Oct 2021, 6:49 AM
Adithya Keshav T
Adithya Keshav T - avatar
+ 2
zemiak why we used \\ before . I didn't understand can you elaborate that for me
14th Oct 2021, 6:52 AM
Adithya Keshav T
Adithya Keshav T - avatar
+ 2
regular expressions (regex) is complex code to exactly define what you want to search and catch in your text https://docs.oracle.com/en/java/javase/16/docs/api dot here mean some char: . Any character (may or may not match line terminators
14th Oct 2021, 7:18 AM
zemiak
0
Thanks Natu for explanation. I know what is a String but now what is this regular expression ? 😟
14th Oct 2021, 7:04 AM
Adithya Keshav T
Adithya Keshav T - avatar
0
String a[]=("76.88").split("."); First variable a is an array of strings. The split method is a string method used to split a string into an array, it takes a string as an argument. 76.88 is a string, the split method will find "." In that string and return an array. [76, 88]
15th Oct 2021, 4:00 PM
Denis Kipkemboi
Denis Kipkemboi - avatar
0
Denis Kipkemboi & Adesewa Adesida in that case the length of array should be 2 but here the length of array is 0
15th Oct 2021, 4:54 PM
Adithya Keshav T
Adithya Keshav T - avatar
0
The main reason is because, by default, unlike. "," and other special characters like spaces. A dot won't work, all you need is to escape "\\." As mention above. Or use regular expression "[.]". This will solve the problem. Now to answer your question is that. The length will be 0 because the string won't be split. And the main reason is because a dot is considered a special character in regular expression.
15th Oct 2021, 7:19 PM
Denis Kipkemboi
Denis Kipkemboi - avatar
0
❤️❤️❤️❤️
15th Oct 2021, 7:58 PM
MuStAfa BoSs