Advertisement
droidus

Untitled

Sep 15th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class school {
  4.   public static void main(String args[]) {
  5.  
  6.  
  7.     String[] array = new String [50];
  8.     String sentence = "There are 87,000,000 people in Canada";
  9.     int sentenceLength = sentence.length();
  10.     int k=0; // counter for the array, "array"
  11.     int h=0; // counter for first time inserting into array -- may not need!!
  12.    
  13.     // STEP ONE
  14.     for (int i = 0; i<sentenceLength; i++) // set up the array with the words
  15.     {
  16.         if (array[k] == null) // make sure to empty the slot of the array element first!!
  17.         {
  18.             array[k] = "";
  19.         }
  20.         else if(sentence.charAt(i) != (' ')) // as long as we haven't reached a space,
  21.         // update the string in the array with the new string
  22.         {
  23.             array[k] = array[k] + sentence.charAt(i);
  24.         }
  25.         else
  26.         {
  27.             k += 1; // go to the next spot to get the next word
  28.         }
  29.     }
  30.  
  31.     for(int j=0; j<array.length; j++) // Print out the array of words
  32.     {
  33.     if(array[j] != null)
  34.     {
  35.         System.out.println(array[j]);
  36.     }
  37.     }
  38.  
  39.   }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement