Results 1 to 7 of 7
  1. #1
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default Need help on logic

    I have a small piece of code which takes a input string, does the cleanup part(removes special characters like '’\\. and replaces any other characters with a space) & then generates a new string.

    Java Code:
    public class Example
    {
    	public static void main(String... args)
    	{
    		charFilter("I.T rocks. It's time to get a job.Come on");
    
    	}
    
    	public static String charFilter(String inText) { 
    
    		String outText="";
    
    		inText = inText.replaceAll("['’\\.]", "");
    		outText = inText.replaceAll("[^a-zA-Z0-9- ]", " ");
    		System.out.println(outText);
    		return outText;
    	}
    
    }
    The output of the above code is "IT rocks Its time to get a jobCome on". But I need to get an output as "IT rocks Its time to get a job Come on"(job & come should appear as separate words, but I.T should appear as IT) because we can expect the user inputting the data to forget adding a space after the full stop.

    Can someone suggest me what approach I need to follow.
    Last edited by nn12; 03-06-2011 at 06:15 PM.

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    I.T rocks. It's time to get a job.Come on
    Only diffrence, I think, is job.Come's dot got a capital letter on the right side of it. and a non capital letter on its left side.
    If so then it should do a space. Otherwise it may remove the dot only.

  3. #3
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Addez your suggestion sounds interesting. Do you happen to know if it can done using regular expression?

  4. #4
    juhiswt is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Just give one more space in outtext string bu which it is being replaced.

  5. #5
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    @juhiswt: the problem with that is then I.T and U.S would appear as I T and U S. I need to keep the abbreviated words together. If we can achieve this then the problem is solved
    Last edited by nn12; 03-07-2011 at 07:44 AM.

  6. #6
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Sorry for late reply. Thought this question was answerd
    so I didnt check it again.

    Well heres how I fixed it,
    Didnt use your fancy way, instead used a forloop:
    PHP Code:
    public class Example
    {
    	public static void main(String... args)
    	{
    		charFilter("I.T rocks. It's time to get a job.Come on");
    
    	}
    
    	public static String charFilter(String inText) { 
    
    		String outText="";
    		for(int i = 0;i < inText.length(); i++){
    			if (inText.charAt(i) == '.' && i > 0){
    				if (!Character.isUpperCase(inText.charAt(i-1)) && Character.isUpperCase(inText.charAt(i+1))){
    					inText = inText.substring(0,i) + " " + inText.substring(i+1);
    				}
    			}
    			
    		}
    		inText = inText.replaceAll("['\\.]", "");
    		outText = inText.replaceAll("[^a-zA-Z0-9- ]", " ");
    		
    		System.out.println(outText);
    		return outText;
    	}
    
    }

  7. #7
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

Similar Threads

  1. Help with logic problem
    By jch02140 in forum New To Java
    Replies: 5
    Last Post: 01-01-2011, 12:39 PM
  2. Logic not working
    By Prajin in forum AWT / Swing
    Replies: 1
    Last Post: 07-19-2010, 08:54 PM
  3. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 03:18 PM
  4. help with a logic error
    By ShinTec in forum Advanced Java
    Replies: 11
    Last Post: 05-02-2010, 11:19 PM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 01:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •