Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does line.split(",")[1] mean [Java]?

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]);
like image 935
SmashCode Avatar asked Apr 04 '16 13:04

SmashCode


People also ask

What does split \\ s+ do in Java?

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.

What is split () function in string?

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.

What is the return type of split () method of string class?

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.


2 Answers

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

like image 181
dryairship Avatar answered Oct 24 '22 16:10

dryairship


It means line is a string beginning with a,b where b is in fact a number.

crtValue is the double value of b.

like image 29
dejvuth Avatar answered Oct 24 '22 16:10

dejvuth