Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By Junky

Thread: Algorithm problem, String splitting

  1. #1
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Algorithm problem, String splitting

    Hey guys, im doing a non assessed worksheet for university but i am struggling on the last section. The brief is as follows:

    "Count the number of words in the rst 200 sentences, and print out the rst and last word from each
    sentence with the number of words between them.
    Sentences are terminated with a full stop, and this should not be included in the last word, when you
    print it out."

    So far i have looked up String trim, String Split etc but i cant see to get it to work.

    I also tried the regex saying if you see a "." or a new line, then store that sentence, then String split the stored sentence but i keep getting arrayindex errors.

    Java Code:
    
    
    public void ReadAndCount(/*File filename*/){
    		try {
    			BufferedReader br = new BufferedReader(new FileReader("D:/Users/Student/Desktop/Java/lines.txt"));
    			for ( int i = 0; i < 5; i++){
    				
    				String line = br.readLine();
    				
    				String[] sent = line.split("[\\.\n]"); 
    				String[] words = sent[1].split("\\s"); //Splits the string based on white space should leave me with each word
    				
                                    System.out.println("Line : " + i + " has this many Words : " + words.length);
    
    			}
    			br.close();
    		}
    		/* Exception will also catch any errors on attempted closure of bufferedReader*/
    
    		catch (IOException e) {
    
    			JOptionPane.showMessageDialog(null, "File not found see ");
    			e.printStackTrace();
    		} 
    	}
    First few lines of the text file being evaluated, just in case you need it

    One reason people lie is to achieve personal power.
    Achieving personal power is helpful for someone who pretends to be more confident than he really is.
    For example, one of my friends threw a party at his house last month. He asked me to come to his party and bring a date.
    However, I didn’t have a girlfriend. One of my other friends, who had a date to go to the party with, asked me about my date.
    I didn’t want to be embarrassed, so I claimed that I had a lot of work to do.
    I said I could easily find a date even better than his if I wanted to.
    I also told him that his date was ugly.
    I achieved power to help me feel confident; however, I embarrassed my friend and his date.
    Although this lie helped me at the time, since then it has made me look down on myself.
    Money causes teenagers to feel stress.
    It makes them feel bad about themselves and envy other people.
    My friend, for instance, lives with her family and has to share a room with her sister, who is very cute and intelligent.
    This girl wishes she could have her own room and have a lot of stuff, but she can’t have these things because her family doesn’t have much money.
    Her family’s income is pretty low because her father is old and doesn’t go to work. Her sister is the only one who works.
    Because her family can’t buy her the things she wants, she feels a lot of stress and gets angry sometimes.
    Last edited by monkeyjr97; 10-01-2013 at 12:24 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default Re: Algorithm problem, String splitting

    i keep getting arrayindex errors.
    Please copy the full text of the error message and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,950
    Rep Power
    4

    Default Re: Algorithm problem, String splitting

    Did you consider just reading in the whole file and then splitting on a period (.)? I also suggest you take care for words which are within a sentence but are adjacent to a comma or a semi-colon or some other punctuation that is not part of the word.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Algorithm problem, String splitting

    in lecture he said he doesnt want the whole file read in, just first 200 sentences. i did an earlier worksheet where you load 20 lines of text in and split it into individual words, but splitting up the sentences is proving difficult.

    Error message

    ļ»æOne reason people lie is to achieve personal power
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at ReadCountAndPrint.ReadAndCount(ReadCountAndPrint.j ava:47)
    at ReadCountAndPrint.<init>(ReadCountAndPrint.java:25 )
    at Main.main(Main.java:7)


    Line 47 is when i try to print something from the sent String[]

    Java Code:
    System.out.println(sent[i]); //CAUSES THE ERROR

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default Re: Algorithm problem, String splitting

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    The code is trying to index an array with less that 4 elements with an index of 3. Remember array indexes range from 0 to the array length-1.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Algorithm problem, String splitting

    i understand the error message but im not sure why the array is only sized at 3, there is 9 words on the first line alone. i cant see why its being read wrong.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Algorithm problem, String splitting

    Try printing out all the elements of the array to see what it actually holds rather than assuming.
    Norm likes this.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default Re: Algorithm problem, String splitting

    Here's an easy way to print an array's contents:
    Java Code:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Algorithm problem, String splitting

    this is the output from the array (after finding the first 5 sentences and printing the array)

    contains: [ļ»æOne reason people lie is to achieve personal power, ]
    contains: [Achieving personal power is helpful for someone who pretends to be more confident than he really is, ]
    contains: [For example, one of my friends threw a party at his house last month, He asked me to come to his party and bring a date, ]
    contains: [However, I didnā€™t have a girlfriend, One of my other friends, who had a date to go to the party with, asked me about my date, ]
    contains: [I didnā€™t want to be embarrassed, so I claimed that I had a lot of work to do, ]



    So the sentences get added to the array fine, so now i need to slit each sentence up into words:

    Java Code:
                                    String[] sent = line.split("[\\.\n]"); 
    				System.out.println("Sentence : " + java.util.Arrays.toString(sent));
    				String[] words = sent[i].split("\\s"); //Splits the string based on white space
    				System.out.println(words.length);
    produces this output:

    9
    0
    0
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at ReadCountAndPrint.ReadAndCount(ReadCountAndPrint.j ava:38)
    at ReadCountAndPrint.<init>(ReadCountAndPrint.java:16 )
    at Main.main(Main.java:7)


    There are 9 words in the first line so it works for the first line


    Now i will toString the word arrays to see what i get:


    results of

    Java Code:
    words : [ļ»æOne, reason, people, lie, is, to, achieve, personal, power]
    words : []
    words : []
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    	at ReadCountAndPrint.ReadAndCount(ReadCountAndPrint.java:38)
    	at ReadCountAndPrint.<init>(ReadCountAndPrint.java:16)
    	at Main.main(Main.java:7)
    So now i have to try and figure out why its not adding any elements to index words[1] onwards

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,950
    Rep Power
    4

    Default Re: Algorithm problem, String splitting

    what is i equal too? Why not just iterate thru the first 5 sentences using a loop and split the words using spaces. And show all your code when you do it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default Re: Algorithm problem, String splitting

    The first print out of the words array shows it contains about 9 elements. The next print outs of the words array shows that it is empty. With an empty array, all index values would be out of bounds.
    You need to see: Why is the words array empty?


    You need to post the current code that produces that output shown here. The posted code does not go with the printed output.
    Last edited by Norm; 10-01-2013 at 03:57 PM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Algorithm problem, String splitting

    i is currently just a for loop ( see first post) im trying to do that but Jim but im struggling a bit.

    im reading the next line as a string, then splitting that into multiple sentences which i store as a string array.

    then i want to split the elements of the sentence array into words based upon word spaces.


    Im up to this point where it will read the first senetence and then spilt it up into into individual words, but after that nothing (see previous post)


    Java Code:
    public void ReadAndCount(){
    		try {
    			BufferedReader br = new BufferedReader(new FileReader("D:/Users/Student/Desktop/Java/lines.txt"));
    			for ( int i = 0; i < 5; i++){
    
    				String line = br.readLine();
    				/* The following expression:
    				 * 
    				 * 		line.split("[\\.\n]") 
    				 * 
    				 * 			will determine a line split if a "." char occurs or if a new line is detected
    				 * 				NOTE: use \\ to signify you are looking for a "." and not just a general char
    				 * 
    				 * 				a "." on its own, specifies any character, \\. specifies that a "." is required
    				 * */
    
    				String[] sent = line.split("[\\.\n]"); 
    				String[] words = sent[i].split("\\s"); //Splits the string based on white space
    				System.out.println("words : " + java.util.Arrays.toString(words));
    				
    			}
    			br.close();
    		}
    		/* Exception will also catch any errors on attempted closure of bufferedReader*/
    
    		catch (IOException e) {
    
    			JOptionPane.showMessageDialog(null, "File not found see ");
    			e.printStackTrace();
    		} 
    	}

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default Re: Algorithm problem, String splitting

    The guts of that method is commented out. What is your purpose for posting that code?

    You need to post a small, complete program that compiles, executes and shows the problem you are having. These bits and pieces are almost useless.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Algorithm problem, String splitting

    its not commented out in my eclipse view, this is the whole method: Updated and almost correct+
    -

    Java Code:
    public void ReadAndCount(){
    		try {
    			BufferedReader br = new BufferedReader(new FileReader("D:/Users/Student/Desktop/Java/lines.txt"));
    			for ( int i = 0; i < 5; i++){
    
    				String line = br.readLine();
    				String[] sent = line.split("[\\.\n]"); 
    
    				for(int j = 0; j < sent.length; j++){
    					String[] words = sent[j].split("\\s"); //Splits the string based on white space
    					System.out.println("words : " + java.util.Arrays.toString(words));
    				}
    			}
    			br.close();
    		}
    produces:

    words : [ļ»æOne, reason, people, lie, is, to, achieve, personal, power]
    words : []
    words : [Achieving, personal, power, is, helpful, for, someone, who, pretends, to, be, more, confident, than, he, really, is]
    words : []
    words : [For, example,, one, of, my, friends, threw, a, party, at, his, house, last, month]
    words : [, He, asked, me, to, come, to, his, party, and, bring, a, date]
    words : []
    words : [However,, I, didnā€™t, have, a, girlfriend]
    words : [, One, of, my, other, friends,, who, had, a, date, to, go, to, the, party, with,, asked, me, about, my, date]
    words : []
    words : [I, didnā€™t, want, to, be, embarrassed,, so, I, claimed, that, I, had, a, lot, of, work, to, do]
    words : []


    Update:

    managed to split in into individual words, but it keeps adding empty arrays, so my lines references are off ( see above output )
    Last edited by monkeyjr97; 10-01-2013 at 04:48 PM.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,950
    Rep Power
    4

    Default Re: Algorithm problem, String splitting

    I believe the reason you are getting an empty line is because you are splitting on a period and a newline character. So for each split you get two values. One is the end of the sentence (via a period) and the other via new line (which is empty). And what happens if your sentence spans two lines (i.e no period in first line but it occurs in the second or third)?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Algorithm problem, String splitting

    Seemed to have solved the issue:

    Java Code:
    public void ReadAndCount(){
    		try {
    			BufferedReader br = new BufferedReader(new FileReader("D:/Users/Student/Desktop/Java/lines.txt"));
    			for ( int i = 0; i < 5; i++){
    
    				String line = br.readLine();
    				String[] sent = line.split("[\\.\n]"); 
    				System.out.println(" \n Sent No : " + ( i + 1));
    
    				for(int j = 0; j < sent.length; j++){
    					String[] words = sent[j].split("\\s"); //Splits the string based on white space
    					for( int k = 0; k < words.length; k++){
    						System.out.print(words[k] + " ");
    					}
    				}
    			}
    			br.close();
    		}

Similar Threads

  1. Splitting the string[]
    By issoa in forum New To Java
    Replies: 1
    Last Post: 02-28-2012, 06:56 AM
  2. Problem splitting a string
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-13-2011, 08:56 PM
  3. Replies: 3
    Last Post: 03-03-2010, 12:23 AM
  4. String Splitting
    By A.M.S in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 07:17 AM
  5. splitting string and replacing
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 03:08 PM

Posting Permissions

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