Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: printing out words

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default printing out words

    i am having trouble printing out words. here is my code, and what happens is, the first letter of every word is kicked off.

    Java Code:
    	String[] array = new String [50];
    	String sentence = "There are 87,000,000 people in Canada";
    	int sentenceLength = sentence.length();
    	int k=0; // counter for the array, "array"
    	int h=0; // counter for first time inserting into array -- may not need!!
    	
    	// STEP ONE	
    	for (int i = 0; i<sentenceLength; i++) // set up the array with the words
    	{
    		if (array[k] == null) // make sure to empty the slot of the array element first!!
    		{
    			array[k] = "";
    		}
    		else if(sentence.charAt(i) != (' ')) // as long as we haven't reached a space, 
    		// update the string in the array with the new string
    		{
    			array[k] = array[k] + sentence.charAt(i);
    		}
    		else
    		{
    			k += 1; // go to the next spot to get the next word
    		}
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: printing out words

    Step through your code with pencil and paper as if you are the computer and see what happens when a new word is encountered, and you will see why your logic is causing your to lose your first letter of each word.
    droidus likes this.

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: printing out words

    sorry; still not seeing it. it should take the old string, and add the new letter to it. still doesn't understand :p

    does it have to do with the "array[k] = array[k] + sentence.charAt(i);", array[k] specific?
    Last edited by droidus; 09-16-2011 at 02:22 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: printing out words

    Quote Originally Posted by droidus View Post
    sorry; still not seeing it. it should take the old string, and add the new letter to it. still doesn't understand :p

    does it have to do with the "array[k] = array[k] + sentence.charAt(i);", array[k] specific?
    Java Code:
    String[] array = new String [50];
    String sentence = "There are 87,000,000 people in Canada";
    int sentenceLength = sentence.length();
    int k=0; 
    
    for (int i = 0; i<sentenceLength; i++) {
      if (array[k] == null) {
         array[k] = "";
      } else if(sentence.charAt(i) != (' ')) {
         array[k] = array[k] + sentence.charAt(i);
      } else {
         k += 1; 
      }
    }
    OK, let's take a little walk, you and I.

    Assume were are the computer and we run into this code. The first several lines are obvious, just loading variables.

    Then the next line we start the for loop at k = 0.
    We know that array[k] is null so the first if is true and k is set to = "";
    Since the first if block runs, then neither else block runs. The 2nd else block doesn't bother me, but the first one does since here i is 0, and sentence.charAt(i) points to the first letter of the String. So the question is: if the first else block doesn't get run, will that first letter ever get placed into the array[k] String? When *will* that else block run? When i = 1 and charAt(i) points to the second letter of the sentence String.

  5. #5
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: printing out words

    oh, i guess i misunderstood the syntax of the coding. i thought that the elseif code would run even if the if statement ran. so i would just have to create 2 different "if" statements?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: printing out words

    Quote Originally Posted by droidus View Post
    oh, i guess i misunderstood the syntax of the coding. i thought that the elseif code would run even if the if statement ran. so i would just have to create 2 different "if" statements?
    You create whatever the logic of the problem dictates should be created.

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: printing out words

    ok, thanks a lot, for clearing this up for me! :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 9
    Last Post: 08-19-2011, 12:59 AM
  2. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 09:17 AM
  3. Help: Num to words
    By MyOnlineChurva in forum New To Java
    Replies: 16
    Last Post: 01-08-2010, 01:41 AM
  4. Replies: 5
    Last Post: 11-22-2008, 01:34 PM
  5. help w words
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 11-21-2007, 06:15 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
  •