import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Counter
{  
public  void split(String a,ArrayList<String> al)
{
char ch[]=a.toCharArray();
int p=0;
for(int i=0;i<ch.length;i++)
{
if(ch[i]==32)
{List<String> l=al.subList(p,i);
ArrayList<String> al1=new ArrayList<String>(l);
//String str=new String[al1.size()];
//al1.toArray(str);
//al1.add(str);
//al1.add(al1);
//why this is also wrong al1.add(al.sublist(p,i));
p=i;
System.out.println(al1);
}
} 
}
public static void main(String... s)
{
String k="My name is Khan Khan";
List<String> l=Arrays.asList(k);
ArrayList<String> al=new ArrayList<String>(l);

Counter c=new Counter();
c.split(k,al);
System.out.println(k);
}
}

ehm... It is missing?

You are the only one who knows what your code is supposed to do, and what it is doing, so you are the only one who can say what is wrong with it.

As for your add method: there isn't any.

Please be specific about what is the actual question. Does it compile? If no, what does the stracktrace/error message say?
Does it run and crash? if so, what does the stacktrace say?
Does it run yet have unexpected output? Please state what the input is, what the expected output is, and what the actual output is.

At this point, all we can do is guess, and nobody is helpded by guessing ..

About the commented code:

String str=new String[al1.size()];

should rather be:
String[] str=new String[al1.size()];

When you reach this:
al1.add(str);

here, you try to add an array of Strings into a list that only accepts elements of the type String. Iterate over the list/array, and add them one by one, but since you already did the toArray(str); , why do you need this?

the same goes for:
al1.add(al1);
which makes even lesser sence.

as for your final line:

al1.add(al.sublist(p,i));

the name of the method is subList, not sublist. Java is case sensitive, so this doesn't give you the same result.

change it to:
al1.add(al.subList(p,i));
at which point you get the exact same error as you did before: you are trying to add a List to a List that only accept instances of String as element.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Counter
{  

{
char ch[]=a.toCharArray();
int p=0;
for(int i=0;i<ch.length;i++)
{
if(ch[i]==32)
{List<String> l=al.subList(p,i);
ArrayList<String> al1=new ArrayList<String>(l);
//String str=new String[al1.size()];
al1.add(al.subList(p,i));
p=i;
System.out.println(al1);
}
} 
}
public static void main(String... s)
{
String k="My name is Khan Khan";
List<String> l=Arrays.asList(k);
ArrayList<String> al=new ArrayList<String>(l);

Counter c=new Counter();
c.split(k,al);
System.out.println(k);
}
}

Check this one 
I tried to count the frequency of word

The explanation of what goes wrong when you call add there is in my previous reply.
You are passing a List to a parameter that can on take a String as parameter.

can u give me the right code using the same add method that splits string into substring with the help of arraylist. Please

splitting a string into 'substring' with the help of arraylist? this makes no sense.
if you mean splitting a String into an array of Strings, depending on a separator, all you need is the split method, why would you need an arraylist for this?

If you want something like this:
al1.add(al.subList(p,i));

you would need to rewrite it something like this:

List<String> temp = al.subList(p, i);
for ( String tempString : temp){
    al1.add(tempString);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.