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

    Default Code review on adding sentences to String array

    What is the best way to form an array of sentences.
    The below code breaks the paragraph into sentences. I want to add each of the sentence into a String array.

    Java Code:
    public class TestExample {
    	public static final String EXAMPLE_TEST = "Wave supports Robots and Gadgets. A robot runs on the server while the gadget runs on the client. The gadget will manipulate the Wave XML locally and the delta is send to the server";
    
    	public static void main(String[] args) {
    		List<String>  list = new ArrayList<String>();
    		BreakIterator bi = BreakIterator.getSentenceInstance();
    		bi.setText(EXAMPLE_TEST);
    		int index = 0;
    		while (bi.next() != BreakIterator.DONE) {
    		    String sentence = EXAMPLE_TEST.substring(index, bi.current());
    		    index = bi.current();
    		    list.add(sentence);
    		    System.out.println("Sentence: " + sentence);
    		   
    		}
    		 String [] SentArr =list.toArray(new String[list.size()]);
    		    System.out.println("SentArr: " + SentArr);
    	}
    	
    }
    I am currently adding it to a list and then converting it back to array.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    You just can use String#split and RegExp and to have the same result.
    Skype: petrarsentev
    http://TrackStudio.com

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

    Default

    The problem of using String split is it would even split out acronyms. I need to keep acronyms together.

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    I thing your code is fair, just small chane

    String sentence...........instead of this create StringBuilder object and create it outside loop
    sanjeev,संजीव

  5. #5
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    So What is this "BreakIterator" in your code? Do you use some special library or it's your code too?
    Skype: petrarsentev
    http://TrackStudio.com

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Um, java.text.BreakIterator
    BreakIterator (Java Platform SE 6)

    db

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

Similar Threads

  1. Receiving code review
    By sunde887 in forum New To Java
    Replies: 0
    Last Post: 03-08-2011, 06:31 AM
  2. Memory Leak questions, code review
    By mensaFool in forum Advanced Java
    Replies: 9
    Last Post: 03-08-2010, 05:27 PM
  3. adding in array String
    By Mekonom in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 05:28 PM
  4. please review the following code
    By ajbj in forum New To Java
    Replies: 3
    Last Post: 08-25-2009, 09:37 AM
  5. [SOLVED] Code review
    By saeedsubedar in forum Advanced Java
    Replies: 14
    Last Post: 06-25-2008, 06:25 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
  •