Can Anyone Explain this code for me | Sololearn: Learn to code for FREE!

+3

Can Anyone Explain this code for me

https://code.sololearn.com/cU8dqh47eQRS/?ref=app

10/14/2021 6:15:28 AM

Adithya Keshav T

13 Answers

New Answer

+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"

+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("\\.");

+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/

+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

+2

Natu the length of array should be 2 but it says 0 for my code

+2

Thanks zemiak split("\\.") is giving the output i expected

+2

zemiak why we used \\ before . I didn't understand can you elaborate that for me

+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

0

Thanks Natu for explanation. I know what is a String but now what is this regular expression ? 😟

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]

0

Denis Kipkemboi & Adesewa Adesida in that case the length of array should be 2 but here the length of array is 0

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.

0

❤️❤️❤️❤️