I came across code where i had encountered with Double.valueOf(line.split(",")[1])
I am familiar with Double.valueOf()
and my problem is to understand what does [1]
mean in the sentence. Searched docs didn't find anything.
while ((line = reader.readLine()) != null)
double crtValue = Double.valueOf(line.split(",")[1]);
split("\\s+") will split the string into string of array with separator as space or multiple spaces. \s+ is a regular expression for one or more spaces.
Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.
As the name suggests, a Java String Split() method is used to decompose or split the invoking Java String into parts and return the Array. Each part or item of an Array is delimited by the delimiters(“”, “ ”, \\) or regular expression that we have passed. The return type of Split is an Array of type Strings.
It means that your line
is a String of numbers separated by commas.
eg: "12.34,45.0,67.1"
The line.split(",")
returns an array of Strings.
eg: {"12.34","45.0","67.1"}
line.split(",")[1]
returns the 2nd(because indexes begin at 0) item of the array.
eg: 45.0
It means line
is a string beginning with a,b
where b
is in fact a number.
crtValue
is the double
value of b
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With