Can not use the split() method

mehdi :

I am trying to split a string which is an array element but the compiler say : type mismatch, can not convert from String[] to String. I don't understand because if we have an array of string the elements of this arrays must be of type String not String[]

This is the code:

while((s=buffereader.readLine())!=null)
{
    words=s.split(" "); 
    for (String word : words) 
    {
        s=words[0];
        s=s.split("T");
    }
}
cricket_007 :

Your compiler is erroring on s=s.split("T"); where s is a string, and not an array.

Simlarly, words.equals(input) doesn't do what you expect because an array will never equal a String. You would need to scan the array to see if any element was equal. If you use a modern IDE rather than compile in the CLI, then you may catch this error quicker.

Assuming you want to check if input is contained within each line, then split the first column on the letter T, this is what you want.

String s;
List<String> words;
while((s=buffereader.readLine()) != null) {
    words = Arrays.asList(s.split("\\s+"));  // Split by one _or more_ spaces
    if (words.contains(input)) {
        s = words.get(0);
        words = Arrays.asList(s.split("T"));
    }
 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=194910&siteId=1