#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1

    Trouble Assigning a Specific Word To An Element in A String Array From A Paragraph


    Hello,

    So I'm doing another exercise from the Ivor Horton book on Beginner Java, and the exercise pretty much wants you to rip words from a paragraph of your choice, and store them into individual elements of an array, sort those elements of that array alphabetically, and print them.

    I'm stuck on the assignment statement in the embedded if loop.

    At this point of the program, I've accumulated letters into a given string, and since I hit a white space, I know that I've reached the end of a word.

    I want to add whatever is the current word to an element of the string_array.

    I tried just assigning the string_array[i] = word;, but that didn't work so I tried maybe using a Constructor to create a new String object from the given word in question, but its giving me errors.

    My error is on line 71 obviously for the reasons I stated above.

    Here is my code, thanks for reading my post! :
    Code:
    public class Chapt4_Exercise3 
    {
    	
    	public static void main(String[] args)
    	
    	{
    		
    		/* 3. Write a program that sets up a String variable containing
    		 * a paragraph of text of your choice. 
    		 * 
    		 * Extract the words from the text and sort them into alphabetical order. 
    		 * 
    		 * Display the sorted list of words. 
    		 * 
    		 * You could use a simple sorting method called the bubble sort. 
    		 * 
    		 * To sort an array into ascending order the process is as follows:
    		 * 
    		 * a. Starting with the first element in the array, compare successive 
    		 * elements (0 and 1, 1 and 2, 2 and 3, and so on).
    		 * 
    		 * b. If the first element of any pair is greater than the second, 
    		 * interchange the two elements.
    		 * 
    		 * c. Repeat the process for the whole array until no interchanges 
    		 * are necessary. 
    		 * 
    		 * The array elements are now in ascending order.
    		 */
    		
    		String paragraph = "Dragon Quest II is set 100 years after the events of the first game. The game's story centers on the prince of Midenhall, who is ordered to stop an evil wizard named Hargon after Hargon destroys Moonbrooke Castle. On his adventure, he is accompanied by his two cousins; the prince of Cannock and the princess of Moonbrooke. Dragon Quest II greatly expands on the series formula from the first game by having a larger party, more areas to explore, multiple heroes and enemies in a battle, and a sailing ship.";
    
    		String [] string_array;
    		
    		String word = "";
    		
    		// Extract the words from the text and sort based on alphabetical
    		// order
    		
    		// Aka create an array that contains the individual words from the
    		// paragraph:
    		
    		// Make a for loop:
    		
    		for (int i = 0; i < paragraph.length(); i++)
    		{
    			// Check for spaces and if they are found, add the current
    			// word to an element inside the string_array:
    			
    			// Make a character variable that will be assigned to the current
    			// character of the string:
    		
    			char ch = paragraph.charAt(i);
    			
    			// Add the current character to an accumulator string that 
    			// will contain a word that we will later add to the string_array:
    			
    			word = word + ch;
    			
    			// Check for white space:
    			
    			if(Character.isWhitespace(ch))
    			{
    				// Since a white space has been found, add the 'word'
    				// to the string_array:
    				
    				string_array[i] = new String(word); 
    				
    			}
    			
    		}
    		
    		
    	}
    
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    What it's complaining about is that you're trying to assign a value to an element of an array. Since that array is a local variable (defined inside the method), you must explicitly initialize the array before you can assign values to its elements.
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    You must define the array with its size before you can assign values to any of its slots.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Ah cool, so I declared the array with the appropriate amount of elements later on.

    I modeled an example in my book that they used to extract words from a given string, and assign it to a subStr array.

    I took this approach, and it prints the words of the Dragon Warrior II paragraph beautifully.

    My main issue is how to construct the for loop near the bottom now that will assign each word in ascending order.


    Here are the errors that I am receiving on line 121 where I am trying to make a condition for the for loop to continue until it goes along the subStr array's entire length:

    Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Cannot invoke length() on the array type String[]
    	The operator < is undefined for the argument type(s) java.lang.String, java.lang.String
    
    	at Chapt4_Exercise3.main(Chapt4_Exercise3.java:121)



    Here is my code:


    Code:
    public class Chapt4_Exercise3 
    {
    	
    	public static void main(String[] args)
    	
    	{
    		
    		/* 3. Write a program that sets up a String variable containing
    		 * a paragraph of text of your choice. 
    		 * 
    		 * Extract the words from the text and sort them into alphabetical order. 
    		 * 
    		 * Display the sorted list of words. 
    		 * 
    		 * You could use a simple sorting method called the bubble sort. 
    		 * 
    		 * To sort an array into ascending order the process is as follows:
    		 * 
    		 * a. Starting with the first element in the array, compare successive 
    		 * elements (0 and 1, 1 and 2, 2 and 3, and so on).
    		 * 
    		 * b. If the first element of any pair is greater than the second, 
    		 * interchange the two elements.
    		 * 
    		 * c. Repeat the process for the whole array until no interchanges 
    		 * are necessary. 
    		 * 
    		 * The array elements are now in ascending order.
    		 */
    		
    		// Declare integer variable for length:
    		int length;
    		
    		// Declare String that will contain the paragraph we want to examine:
    		String paragraph = "Dragon Quest II is set 100 years after the events of the first game. The game's story centers on the prince of Midenhall, who is ordered to stop an evil wizard named Hargon after Hargon destroys Moonbrooke Castle. On his adventure, he is accompanied by his two cousins; the prince of Cannock and the princess of Moonbrooke. Dragon Quest II greatly expands on the series formula from the first game by having a larger party, more areas to explore, multiple heroes and enemies in a battle, and a sailing ship.";
    
    		// Declare the integer variable used for the count:
    		int count = 0; 
    		
    		// Declare the character variable that will be used as the separator
    		// between words:
    		char separator = ' '; 
    		
    		// Determine the number of substrings by first declaring an integer
    		// index variable:
    		int index = 0;
    		
    		// Create a do while loop that will examine the entire paragraph:
    		do 
    		{ 
    			// Increment the count and index values:
    			++count; 
    			++index; 
    			// Assign the current index to the next word inside the paragraph:
    			index = paragraph.indexOf(separator, index);
    		} while (index != -1); 
    		
    		// Extract the substring into an array: 
    		String[] subStr = new String[count]; 
    		
    		// Create integer variables for the start index and the end index 
    		// of the subStr String array[]:
    		index = 0; 
    		int endIndex = 0; 
    		
    		// Create a for loop that will place values into the subStr array:
    		for(int i = 0; i < count; ++i) 
    		{ 
    			// Assign the end index to the end of the current word:
    			endIndex = paragraph.indexOf(separator, index); 
    			
    			// Create an if loop that will check to see when the endIndex
    			// is a negative value aka when it has reached the end of the
    			// paragraph:
    			if (endIndex == -1) 
    			{ 
    				// Assign the current element of the subStr array[] 
    				// using the .substring() method:
    				subStr[i] = paragraph.substring(index); 
    				
    			} 
    			// Create an else statement that will add normal words inside
    			// the paragraph to the subStr[] array:
    			else 
    			{ 
    				// Use the.substring() method to add the current
    				// word to the subStr[] array:
    				subStr[i] = paragraph.substring(index, endIndex); 
    				
    			} 
    			
    			// Increment the current endIndex by 1:
    			index = endIndex + 1; 
    			
    		} 
    		
    		// Output statement using a collection-based for loop:
    		for(String s : subStr) 
    		{ 
    			System.out.println(s); 
    			
    		} 
    		
    		// Now actually compare the current elements inside the subStr[] array:
    		/* Book's approach to this problem:
    		 * To sort an array into ascending order the process is as follows:
    		 * 
    		 * a. Starting with the first element in the array, compare successive 
    		 * elements (0 and 1, 1 and 2, 2 and 3, and so on).
    		 * 
    		 * b. If the first element of any pair is greater than the second, 
    		 * interchange the two elements.
    		 * 
    		 * c. Repeat the process for the whole array until no interchanges 
    		 * are necessary. 
    		 */
    		
    		// Create a for loop that will look at each element:
    		
    		for (int i = 0; i < subStr.length(); i++)
    		{
    			
    			// Make an if statement that will compare the current
    			// element of subStr with the next element:
    			if(subStr[i] < subStr[i+1])
    			{
    				subStr[i+1] = subStr[i];
    			
    			}
    			
    			
    			
    		}
    		
    	}
    			
    }
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    assign each word in ascending order.
    You could sort the Strings by putting the Strings into an ArrayList and use the Collections class's sort() method.

    operator < is undefined for the argument type(s) java.lang.String,
    You need to use methods for comparing Strings. The operators: <, >, == etc only work with primitives.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    It works great, but should I also sort it with uppercase and lowercase as well?

    Here's my code:

    Code:
    import java.util.Arrays;
    
    public class Chapt4_Exercise3 
    {
    	
    	public static void main(String[] args)
    	
    	{
    		
    		/* 3. Write a program that sets up a String variable containing
    		 * a paragraph of text of your choice. 
    		 * 
    		 * Extract the words from the text and sort them into alphabetical order. 
    		 * 
    		 * Display the sorted list of words. 
    		 * 
    		 * You could use a simple sorting method called the bubble sort. 
    		 * 
    		 * To sort an array into ascending order the process is as follows:
    		 * 
    		 * a. Starting with the first element in the array, compare successive 
    		 * elements (0 and 1, 1 and 2, 2 and 3, and so on).
    		 * 
    		 * b. If the first element of any pair is greater than the second, 
    		 * interchange the two elements.
    		 * 
    		 * c. Repeat the process for the whole array until no interchanges 
    		 * are necessary. 
    		 * 
    		 * The array elements are now in ascending order.
    		 */
    		
    		// Declare integer variable for length:
    		int length;
    		
    		// Declare String that will contain the paragraph we want to examine:
    		String paragraph = "Dragon Quest II is set 100 years after the events of the first game. The game's story centers on the prince of Midenhall, who is ordered to stop an evil wizard named Hargon after Hargon destroys Moonbrooke Castle. On his adventure, he is accompanied by his two cousins; the prince of Cannock and the princess of Moonbrooke. Dragon Quest II greatly expands on the series formula from the first game by having a larger party, more areas to explore, multiple heroes and enemies in a battle, and a sailing ship.";
    
    		// Declare the integer variable used for the count:
    		int count = 0; 
    		
    		// Declare the character variable that will be used as the separator
    		// between words:
    		char separator = ' '; 
    		
    		// Determine the number of substrings by first declaring an integer
    		// index variable:
    		int index = 0;
    		
    		// Create a do while loop that will examine the entire paragraph:
    		do 
    		{ 
    			// Increment the count and index values:
    			++count; 
    			++index; 
    			// Assign the current index to the next word inside the paragraph:
    			index = paragraph.indexOf(separator, index);
    		} while (index != -1); 
    		
    		// Extract the substring into an array: 
    		String[] subStr = new String[count]; 
    		
    		// Create integer variables for the start index and the end index 
    		// of the subStr String array[]:
    		index = 0; 
    		int endIndex = 0; 
    		
    		// Create a for loop that will place values into the subStr array:
    		for(int i = 0; i < count; ++i) 
    		{ 
    			// Assign the end index to the end of the current word:
    			endIndex = paragraph.indexOf(separator, index); 
    			
    			// Create an if loop that will check to see when the endIndex
    			// is a negative value aka when it has reached the end of the
    			// paragraph:
    			if (endIndex == -1) 
    			{ 
    				// Assign the current element of the subStr array[] 
    				// using the .substring() method:
    				subStr[i] = paragraph.substring(index); 
    				
    			} 
    			// Create an else statement that will add normal words inside
    			// the paragraph to the subStr[] array:
    			else 
    			{ 
    				// Use the.substring() method to add the current
    				// word to the subStr[] array:
    				subStr[i] = paragraph.substring(index, endIndex); 
    				
    			} 
    			
    			// Increment the current endIndex by 1:
    			index = endIndex + 1; 
    			
    		} 
    		
    		// Output statement using a collection-based for loop:
    		for(String s : subStr) 
    		{ 
    			System.out.println(s); 
    			
    		} 
    		
    		// Now actually compare the current elements inside the subStr[] array:
    		/* Book's approach to this problem:
    		 * To sort an array into ascending order the process is as follows:
    		 * 
    		 * a. Starting with the first element in the array, compare successive 
    		 * elements (0 and 1, 1 and 2, 2 and 3, and so on).
    		 * 
    		 * b. If the first element of any pair is greater than the second, 
    		 * interchange the two elements.
    		 * 
    		 * c. Repeat the process for the whole array until no interchanges 
    		 * are necessary. 
    		 */
    		
    		// Use the .sort() method from the Arrays Collection class:
    		Arrays.sort(subStr);
    		
    		// Print the elements of the array
    		System.out.println("The new sorted array is as follows: ");
    		
    		// Make a collection based for loop again that will create a string
    		// variable called s that will be assigned to the current element
    		// of the subStr array:
    		for(String s : subStr)
    		{
    			System.out.println(s); 
    			
    		}
    		
    		
    		
    		
    		
    	}
    			
    }
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    What way makes the most sense for the user looking at the output?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Hmm, It would be pretty cool to have the Uppercase A's along with the lower case a's together, but honestly, in terms of precedence, Capital letter based words will have more precedence as they are more important.

    Its pretty funny because grammatically, you would match the a's, but in this case, it kind of makes sense in some sort of weird way.

    I guess it is up to me honestly. At this point, I wouldn't want to necessarily "give up" but it makes sense at this point honestly.

    Good point, thanks for your posts man!
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    The "natural order" of ASCII letters is: A-Z before a-z
    so Banana comes before apple

    If you want to change what the sort() method does, you can write your own Comparator method to control what order Strings are sorted in.

IMN logo majestic logo threadwatch logo seochat tools logo