简体   繁体   中英

The method split(String) is undefined for the type List<String>

I am making a small program to split sentences when detecting a dot

I am struggling to print the result

// print result list
for(int i = 0; i < fileContent.size(); i++) {
    String[] fileContent1 = (fileContent).split("\\.");
}             

the function split is not working and on eclipse I got this error message : The method split(String) is undefined for the type List

I understand the error message, I tried to cast the result but not working.

Do you have any ideas ?

Thank you for your explanations and help :)

Update 1

Thank for your reply,

I think it is better to put the full loop to understand better Indeed I made a mistake its a list of String

// print result list

for(int i = 0; i < fileContent.size(); i++) 
{
    List<String> fileContent1 = fileContent.split("\\.");

    System.out.println(fileContent.get(i));     

    PrintWriter out = new PrintWriter(new FileWriter("C:/Users/Jer/Desktop/outputfile.txt")); 
    //out.print("Hello "); 
    out.print(fileContent); 
    out.close();
}

Update 2 :

Well I got any error message :) thank you very much I will continue to debug my code on my side now

Change your code to

for(String singleFileContent: fileContent) {
    String[] fileContent1 = singleFileContent.split("\\.");
}

Seems like the fileContent is a List and not a String.

You code also do it like (as mention in an other answer)

for(int i = 0; i < fileContent.size(); i++) {
   String[] fileContent1 = fileContent.get(i).split("\\.");
}

Judging from your error message, I'm guessing fileContent is of type List<String> . If so, you probably want to do something like:

for (String fc : fileContent) {
    String[] fileContent1 = fc.split("\\.");
    // ...
}

fileContent is of type List and not String , as indicated in the compiler message. The split method should be called against a String object. If the list contains String elements, and you want to split each element in the list, you need to get the element first:

for(int i = 0; i < fileContent.size(); i++) {
       String[] fileContent1 = fileContent.get(i).split("\\.");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM