Page 1 of 1

Question with string :)

Posted: Fri Nov 13, 2015 3:55 pm
by KGB1st

Code: Select all

String[] data = event.split(" ");
String[] val = data[1];
Why in 1st code data sets to the string value, but in 2nd method I got ex about that it can't convert? :really:

Re: Question with string :)

Posted: Sat Nov 14, 2015 8:01 am
by LasTravel
Because data[1] is a String, not a String[], so you should use:

Code: Select all

String val = data[1];

Re: Question with string :)

Posted: Sat Nov 14, 2015 9:13 am
by KGB1st
LasTravel wrote:Because data[1] is a String, not a String[], so you should use:

Code: Select all

String val = data[1];
aaa.. this's such as dynmic sting (array)?

Re: Question with string :)

Posted: Sat Nov 14, 2015 10:25 am
by SaveGame
Java is strictly typed. String#split(String) returns String[], which is a simple array.
A String in Java is not an array (although in typical JDK implementations there is a cached char[] underneath).

Consider

Code: Select all

char[] val = data[1].toCharArray()