Click here to Skip to main content
16,018,805 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public class stringtowords 
{  
	public static void Clear(String s)
	{
		String wo[]=s.split(" ");
	       for(String w:wo)
	      {
	    	  System.out.println(w);
	      }
	       System.out.println("clear words i.e. after ignoring special characters and no. and converting all to small letters are:");
	      	
	      String word;
	       for(int i=0;i<wo.length;i++)
	       { 
	    	   word=wo[i].toLowerCase();
	    	    List<Character> li=new ArrayList<Character>(); 
	    	    for (char ch:word.toCharArray())
	    	      if(ch>='a' && ch<='z')
	    	      li.add(ch);
	    	    String ss="";
	    	    for(char ch:li)
	    	    ss=ss+ch;
	    	    
	    	    System.out.println(ss);
	      }
	}
	public static void main(String args[])
	{
	   String s="Hello! my my Name@ is Pragya12";
       Clear(s);
      // System.out.println(g);
       
     }    
}


What I have tried:

the above code is working fine but instead of using void return type in my Clear function,I want to use String return type but I'm not getting it how.Any help would be appreciated.
Posted
Updated 14-Sep-18 7:06am

1 solution

public static String Clear(String s)
	{
		String sReturn = "";
		String wo[]=s.split(" ");
	       for(String w:wo)
	      {
	    	  return w; // Maybe you need a lineSpace
	      }
	       sReturn = "clear words i.e. after ignoring special characters and no. and converting all to small letters are:";
	      	
	      String word;
	       for(int i=0;i<wo.length;i++)
	       { 
	    	   word=wo[i].toLowerCase();
	    	    List<Character> li=new ArrayList<Character>(); 
	    	    for (char ch:word.toCharArray())
	    	      if(ch>='a' && ch<='z')
	    	      li.add(ch);
	    	    String ss="";
	    	    for(char ch:li)
	    	    ss=ss+ch;
	    	    
	    	    sReturn += ss; // Maybe you need a lineSpace
	      }

	      return sReturn;
	}
 
Share this answer
 
v2
Comments
Member 13954890 14-Sep-18 13:18pm    
no it's showing error-'The method ust return result of type string'. In output i want clear words i.e. after ignoring special characters and no. and converting all to small letters like :
Hello!
my
my
Name@
is
Pragya12
clear words i.e. after ignoring special characters and no. and converting all to small letters are:
hello
my
my
name
is
pragya
Matias Lopez 14-Sep-18 13:30pm    
When you call to Clear(String s), you bulk the result into a String variable, no?
Member 13954890 14-Sep-18 13:37pm    
Sir i'm really not getting how to do it.I tried a lot but each and every time i got struck.Will you please help me out?It's working quite fine with void. I guess there is some problem with for loop when using string as my return type but however i'm not getting how to code it when using string as my return type?
.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900