Results 1 to 7 of 7
Thread: Need help on logic
- 03-06-2011, 05:12 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
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.
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.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; } }
Can someone suggest me what approach I need to follow.Last edited by nn12; 03-06-2011 at 05:15 PM.
- 03-06-2011, 09:54 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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.
- 03-07-2011, 05:30 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
Addez your suggestion sounds interesting. Do you happen to know if it can done using regular expression?
- 03-07-2011, 06:21 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Just give one more space in outtext string bu which it is being replaced.
- 03-07-2011, 06:41 AM #5
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
@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 06:44 AM.
- 03-09-2011, 07:01 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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; } }
- 03-10-2011, 11:06 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
Similar Threads
-
Help with logic problem
By jch02140 in forum New To JavaReplies: 5Last Post: 01-01-2011, 11:39 AM -
Logic not working
By Prajin in forum AWT / SwingReplies: 1Last Post: 07-19-2010, 07:54 PM -
need a logic for this
By rajivjoshi in forum New To JavaReplies: 4Last Post: 06-12-2010, 02:18 PM -
help with a logic error
By ShinTec in forum Advanced JavaReplies: 11Last Post: 05-02-2010, 10:19 PM -
Cant get the logic right
By jermaindefoe in forum New To JavaReplies: 4Last Post: 03-11-2008, 12:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks