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

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Oh wow, I think the problem lies within the index = 0 statement.

    I learned how to use debugging in the Eclipse IDE by using breakpoints, and I noticed that index keeps being reassigned to 0.

    The for loop keeps looping as written, and as you said, the do while loop only executes once which would be okay to look at a given string inside the "This is a test" array.

    What should I do then? Should I assign index to be zero at the top of my code to prevent it from being reassigned to zero every time it goes through the for loop before the actual do-while loop?
  2. #17
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    What should I do then?
    I don't think you have spent enough time designing for this project.
    Can you make a list of the steps the program must take to solve this problem?
    I don't think you have such a list and are trying to write code rather randomly instead of coding to a design.

    When you have a list, then work on the items in the list one at a time.
    What steps have you finished so far?
    What step are you now working on?
    Last edited by NormR; November 9th, 2013 at 02:16 PM.
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by NormR
    I don't think you have spent enough time designing for this project.
    Can you make a list of the steps the program must take to solve this problem?
    I don't think you have such a list and are trying to write code rather randomly instead of coding to a design.

    When you have a list, then work on the items in the list one at a time.
    What steps have you finished so far?
    What step are you now working on?
    I don't really know what to do though, that's the issue.

    I'm only trying to base my ideas off similar examples in the book, but obviously that's not working.

    The instructions for the question are one thing, but they don't really imply what to do exactly.

    I want to really just ditch this do-while loop and kick it to the curb honestly. I think I might try this with a for loop instead.

    You know what, I'm gonna delete the do while loop and see if I can do this with a for loop instead.
  6. #19
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I don't really know what to do
    That is a problem. You are wasting your time trying to write code if you don't know what you are trying to do.

    Given the sentence: "This is it"
    Is the objective: "sihT si ti"

    Steps:
    Initialize output String
    in a loop
    1) isolate the next word
    2) reverse that word
    3) add it to the output String
    end loop
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    So I did ditch the ****ing do-while loop and found out that its not even going through each character anyway.

    My second dimension charAtSubIndex doesn't correspond to a character inside the given string.

    I'm confused on why its not printing out the current character on line 117 of my code...

    Here's my code:
    Code:
    public class Chapt4_Exercise5 
    {
    	
    	public static void main (String[] args)
    	
    	{
    		/* 5. Write a program that reverses the sequence of letters in each 
    		 * word of your chosen paragraph from Exercise 3. 
    		 * 
    		 * For instance, "To be or not to be." becomes "oT eb ro ton ot eb." 
    		 */
    		
    		// Old Paragraph String:
    		// String paragraph = "Far best is he who knows all things himself. Good, he that hearkens when men counsel right. But he who neither knows, nor lays to heart. Another's wisdom, is a useless wight.";	
    		
    		String paragraph = "This is a test";
    		
    		// Declare integer variable for length:
    		int length;
    				
    	
    		// 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); 
    			
    		} 
    		
    		
    		// REVERSE SECTION:
    		
    		System.out.println("NOW ENTERING REVERSE SECTION: ");
    		
    		// Make a String array that will hold the reverse forms of the
    		// elements found in the subStr[] array:
    		String[] reverse_array = new String[count]; 
    		
    		// Make an two overarching for loops before the do-while loop
    		
    		// First dimension for
    		for(int subIndex = 0; subIndex < subStr.length; subIndex++)
    		{
    			
    		// Search deeper with the second dimension:
    			for(int charInSubIndex = 0; charInSubIndex < subStr[subIndex].length(); 
    					charInSubIndex++)
    			{
    				
    		
    				// DEBUGGING SECTION:
    				
    				System.out.println("Current string: " + subStr[subIndex]);
    				
    				// Why is not printing out the current character of subStr here? :
    				System.out.println("Current character: " + subStr[charInSubIndex]);
    				
    				
    			
    				
    			}
    		
    		}
    				
    		// Create integer variables for the start index and the end index 
    		// of the subStr String array[]:
    		
    		// COMMENT OUT THE REST OF THE CODE TO TEST DO-WHILE LOOP:
    		/*
    		index = 0; 
    		endIndex = 0; 
    		
    		// Somehow make a for loop that will cycle through each
    		// substring element, and reverse the sequence of letters of
    		// the current word:
    		for (i = 0; i < count; i++)
    		{
    			// Assign the end index to the end of the current word being
    			// examined:
    			
    			endIndex = subStr[i].indexOf(separator, index);
    			
    			// Check to see if the end of the substring has been
    			// reached:
    			
    			if (endIndex == -1)
    			{ 
    				// This is where I want to actually add the reverse
    				// letters in order, so think think think on how
    				// to reverse the order
    				
    				// Somehow I have to use the last element of the
    				// substring and add it to the new array... 
    				
    				// Maybe take the entire length, and subtract it by the 
    				// index in the forward direction to get the reverse letters?...
    				
    				reverse_array[i] = subStr[i].substring(subStr.length-1);		
    			
    			}
    				
    				
    		
    		
    		}
    		
    		*/
    
        }
    	
    }
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by NormR
    That is a problem. You are wasting your time trying to write code if you don't know what you are trying to do.

    Given the sentence: "This is it"
    Is the objective: "sihT si ti"

    Steps:
    Initialize output String
    in a loop
    1) isolate the next word
    2) reverse that word
    3) add it to the output String
    end loop
    Good point, but look at my code above, the for loop doesn't even go through each current character anyway.

    Cool, thank you for the steps to go by. They are pretty good, but do I use a for loop for this or a do-while loop?

    God knows I suck at Do-While loops lol.
  12. #22
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    If there is always a single space between the words or it doesn't matter if extra spaces are lost, the String class has a method: split() that will separate all the words.
  14. #23
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    ^ Alright, I'll look at the split() method if its in my book or some online documentation.

    But is there anyway to force it to print out the current character being examined?

    The only possibility I could think of was to make it look at the two dimensions via the println statement like this:

    System.out.println("Current character: " + subStr[subIndex][charInSubIndex]);

    using two pairs of brackets to specify the specific dimension I want.

    I added a print line statement for length, so its matching up with the current string to be examined, which is fine.

    Its just the matter of printing the current character that's troubling me.

    Here's my code:
    Code:
    public class Chapt4_Exercise5 
    {
    	
    	public static void main (String[] args)
    	
    	{
    		/* 5. Write a program that reverses the sequence of letters in each 
    		 * word of your chosen paragraph from Exercise 3. 
    		 * 
    		 * For instance, "To be or not to be." becomes "oT eb ro ton ot eb." 
    		 */
    		
    		// Old Paragraph String:
    		// String paragraph = "Far best is he who knows all things himself. Good, he that hearkens when men counsel right. But he who neither knows, nor lays to heart. Another's wisdom, is a useless wight.";	
    		
    		String paragraph = "This is a test";
    		
    		// Declare integer variable for length:
    		int length;
    				
    	
    		// 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); 
    			
    		} 
    		
    		
    		// REVERSE SECTION:
    		
    		System.out.println("NOW ENTERING REVERSE SECTION: ");
    		
    		// Make a String array that will hold the reverse forms of the
    		// elements found in the subStr[] array:
    		String[] reverse_array = new String[count]; 
    		
    		// Make an two overarching for loops before the do-while loop
    		
    		// First dimension for
    		for(int subIndex = 0; subIndex < subStr.length; subIndex++)
    		{
    			
    		// Search deeper with the second dimension:
    			for(int charInSubIndex = 0; charInSubIndex < subStr[subIndex].length(); 
    					charInSubIndex++)
    			{
    				
    		
    				// DEBUGGING SECTION:
    				
    				System.out.println("Current string: " + subStr[subIndex]);
    				
    				// Why is not printing out the current character of subStr here? :
    				System.out.println("Current character: " + subStr[charInSubIndex]);
    				
    				System.out.println("Length = " + subStr[subIndex].length());
    				
    			
    				
    			}
    		
    		}
    				
    		// Create integer variables for the start index and the end index 
    		// of the subStr String array[]:
    		
    		// COMMENT OUT THE REST OF THE CODE TO TEST DO-WHILE LOOP:
    		/*
    		index = 0; 
    		endIndex = 0; 
    		
    		// Somehow make a for loop that will cycle through each
    		// substring element, and reverse the sequence of letters of
    		// the current word:
    		for (i = 0; i < count; i++)
    		{
    			// Assign the end index to the end of the current word being
    			// examined:
    			
    			endIndex = subStr[i].indexOf(separator, index);
    			
    			// Check to see if the end of the substring has been
    			// reached:
    			
    			if (endIndex == -1)
    			{ 
    				// This is where I want to actually add the reverse
    				// letters in order, so think think think on how
    				// to reverse the order
    				
    				// Somehow I have to use the last element of the
    				// substring and add it to the new array... 
    				
    				// Maybe take the entire length, and subtract it by the 
    				// index in the forward direction to get the reverse letters?...
    				
    				reverse_array[i] = subStr[i].substring(subStr.length-1);		
    			
    			}
    				
    				
    		
    		
    		}
    		
    		*/
    
        }
    	
    }
    Last edited by HauntJemimah; November 11th, 2013 at 01:56 PM.
  16. #24
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Alright so this is what I found out about the .split() method. It was mentioned in Chapter 4, so good point. It takes in two arguments.

    1. a String object that is the pattern for the delimiter or separator (in this case, a slash mark).

    2. An integer value that is the count of the maximum number of times that the pattern can be applied to find tokens (or in this case, words).

    Wouldn't that second argument be three in this case because I want to find the day, month and then year for the date?

    So in use of my date string, would the following be correct?

    (I already declared the string in the above code)

    // Make a new array:

    String [] parts_of_date = date.split("[/]", 3);

    Wouldn't this create a new array with three elements?
  18. #25
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Wouldn't this create a new array with three elements?
    What happens when the code is compiled and executed? Doing it will show you what it does.

    You can use the Arrays class's toString() method to format the array for printing;
    Last edited by NormR; November 11th, 2013 at 02:09 PM.
  20. #26
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    I tried using the .split method, but in the output statement, I get this weird hexadecimal code for the actual array.

    Did I use the .split method incorrectly or did I use the .toString() method incorrectly.

    When I tried using .split(), intellisense built into the Eclipse IDE told me that it wanted "regex" for some reason.

    EDIT: Ah, so regex means regular expression. So I'm guessing the regular expression in this case would be the delimiter itself?


    Here's my code:
    Code:
    import java.sql.Date;
    
    public class Chapt4_Exercise5 
    {
    	
    	public static void main (String[] args)
    	
    	{
    		/* 5. Write a program that reverses the sequence of letters in each 
    		 * word of your chosen paragraph from Exercise 3. 
    		 * 
    		 * For instance, "To be or not to be." becomes "oT eb ro ton ot eb." 
    		 */
    		
    		// Old Paragraph String:
    		// String paragraph = "Far best is he who knows all things himself. Good, he that hearkens when men counsel right. But he who neither knows, nor lays to heart. Another's wisdom, is a useless wight.";	
    		
    		String paragraph = "This is a test";
    		
    		// Declare ineger variable for length:
    		int length;
    				
    	
    		// 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); 
    			
    		} 
    		
    		
    		// REVERSE SECTION:
    		
    		System.out.println("NOW ENTERING REVERSE SECTION: ");
    		
    		// Make a String array that will hold the reverse forms of the
    		// elements found in the subStr[] array:
    		String[] reverse_array = new String[count]; 
    		
    		// Make an two overarching for loops before the do-while loop
    		
    		// First dimension for
    		for(int subIndex = 0; subIndex < subStr.length; subIndex++)
    		{
    			
    		// Search deeper with the second dimension:
    			for(int charInSubIndex = 0; charInSubIndex < subStr[subIndex].length(); 
    					charInSubIndex++)
    			{
    				// DEBUGGING SECTION:
    				
    				System.out.println("Current string: " + subStr[subIndex]);
    				
    				// Why is not printing out the current character of subStr here? :
    				System.out.println("Current character: " + subStr[charInSubIndex]);
    				
    				System.out.println("Length = " + subStr[subIndex].length());
    				
    				// Using A Tokenizer is on p. 149:
    				String [] parts_of_paragraph = paragraph.split("[/]", 3);
    				
    				// Print the array using toString()
    				System.out.println("This is the parts_of_paragraph array: " + parts_of_paragraph.toString());
    				
    			}
    		
    		}
    				
    		// Create integer variables for the start index and the end index 
    		// of the subStr String array[]:
    		
    		// COMMENT OUT THE REST OF THE CODE TO TEST DO-WHILE LOOP:
    		/*
    		index = 0; 
    		endIndex = 0; 
    		
    		// Somehow make a for loop that will cycle through each
    		// substring element, and reverse the sequence of letters of
    		// the current word:
    		for (i = 0; i < count; i++)
    		{
    			// Assign the end index to the end of the current word being
    			// examined:
    			
    			endIndex = subStr[i].indexOf(separator, index);
    			
    			// Check to see if the end of the substring has been
    			// reached:
    			
    			if (endIndex == -1)
    			{ 
    				// This is where I want to actually add the reverse
    				// letters in order, so think think think on how
    				// to reverse the order
    				
    				// Somehow I have to use the last element of the
    				// substring and add it to the new array... 
    				
    				// Maybe take the entire length, and subtract it by the 
    				// index in the forward direction to get the reverse letters?...
    				
    				reverse_array[i] = subStr[i].substring(subStr.length-1);		
    			
    			}
    				
    				
    		
    		
    		}
    		
    		*/
    
        }
    	
    }
    Here's my output:

    Code:
    This
    is
    a
    test
    NOW ENTERING REVERSE SECTION: 
    Current string: This
    Current character: This
    Length = 4
    This is the parts_of_paragraph array: [Ljava.lang.String;@19ee1ac
    Current string: This
    Current character: is
    Length = 4
    This is the parts_of_paragraph array: [Ljava.lang.String;@1f1fba0
    Current string: This
    Current character: a
    Length = 4
    This is the parts_of_paragraph array: [Ljava.lang.String;@1befab0
    Current string: This
    Current character: test
    Length = 4
    This is the parts_of_paragraph array: [Ljava.lang.String;@13c5982
    Current string: is
    Current character: This
    Length = 2
    This is the parts_of_paragraph array: [Ljava.lang.String;@1186fab
    Current string: is
    Current character: is
    Length = 2
    This is the parts_of_paragraph array: [Ljava.lang.String;@14b7453
    Current string: a
    Current character: This
    Length = 1
    This is the parts_of_paragraph array: [Ljava.lang.String;@c21495
    Current string: test
    Current character: This
    Length = 4
    This is the parts_of_paragraph array: [Ljava.lang.String;@1d5550d
    Current string: test
    Current character: is
    Length = 4
    This is the parts_of_paragraph array: [Ljava.lang.String;@c2ea3f
    Current string: test
    Current character: a
    Length = 4
    This is the parts_of_paragraph array: [Ljava.lang.String;@a0dcd9
    Current string: test
    Current character: test
    Length = 4
    This is the parts_of_paragraph array: [Ljava.lang.String;@1034bb5
  22. #27
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    [Ljava.lang.String;@14b7453
    That is what the toString() method for a String array returns when you reference the array as a String for printing. If you want to see the contents of the array use the Arrays class's toString() method:
    Code:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
  24. #28
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Actually, I realized what the problem was.

    I was using slash mark '/' delimiters for .split() just like my tokenizing_dates project --> aka problem 1.

    Also, when I printed an individual element, I didn't index into the array itself.

    So I added a for loop INSIDE the second for loop just for printing the elements of the parts_of_paragraph array.

    And its fine except for one thing, 'a test' is considered to be one element, but it should have been split into two parts.

    Should I add another space delimiter, or run .split again in an if else clause that will catch an exception where a single character has a space following it and preceding it?

    Here's my code:
    Code:
    import java.sql.Date;
    
    public class Chapt4_Exercise5 
    {
    	
    	public static void main (String[] args)
    	
    	{
    		/* 5. Write a program that reverses the sequence of letters in each 
    		 * word of your chosen paragraph from Exercise 3. 
    		 * 
    		 * For instance, "To be or not to be." becomes "oT eb ro ton ot eb." 
    		 */
    		
    		// Old Paragraph String:
    		// String paragraph = "Far best is he who knows all things himself. Good, he that hearkens when men counsel right. But he who neither knows, nor lays to heart. Another's wisdom, is a useless wight.";	
    		
    		String paragraph = "This is a test";
    		
    		// Declare ineger variable for length:
    		int length;
    				
    	
    		// 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); 
    			
    		} 
    		
    		
    		// REVERSE SECTION:
    		
    		System.out.println("NOW ENTERING REVERSE SECTION: ");
    		
    		// Make a String array that will hold the reverse forms of the
    		// elements found in the subStr[] array:
    		String[] reverse_array = new String[count]; 
    		
    		// Make an two overarching for loops before the do-while loop
    		
    		// First dimension for
    		for(int subIndex = 0; subIndex < subStr.length; subIndex++)
    		{
    			
    		// Search deeper with the second dimension:
    			for(int charInSubIndex = 0; charInSubIndex < subStr[subIndex].length(); 
    					charInSubIndex++)
    			{
    				// DEBUGGING SECTION:
    				
    				System.out.println("Current string: " + subStr[subIndex]);
    				
    				// Why is not printing out the current character of subStr here? :
    				System.out.println("Current character: " + subStr[charInSubIndex]);
    				
    				System.out.println("Length = " + subStr[subIndex].length());
    				
    				// Using A Tokenizer is on p. 149:
    				String [] parts_of_paragraph = paragraph.split("[ ]", 3);
    				
    				// Print out the parts of the parts_of_paragraph array for debugging:
    				for(int i = 0; i < parts_of_paragraph.length; i++)
    				{
    					System.out.println("Current element of parts: " + parts_of_paragraph[i]);
    					
    				}
    				
    				
    			}
    		
    		}
    				
    		// Create integer variables for the start index and the end index 
    		// of the subStr String array[]:
    		
    		// COMMENT OUT THE REST OF THE CODE TO TEST DO-WHILE LOOP:
    		/*
    		index = 0; 
    		endIndex = 0; 
    		
    		// Somehow make a for loop that will cycle through each
    		// substring element, and reverse the sequence of letters of
    		// the current word:
    		for (i = 0; i < count; i++)
    		{
    			// Assign the end index to the end of the current word being
    			// examined:
    			
    			endIndex = subStr[i].indexOf(separator, index);
    			
    			// Check to see if the end of the substring has been
    			// reached:
    			
    			if (endIndex == -1)
    			{ 
    				// This is where I want to actually add the reverse
    				// letters in order, so think think think on how
    				// to reverse the order
    				
    				// Somehow I have to use the last element of the
    				// substring and add it to the new array... 
    				
    				// Maybe take the entire length, and subtract it by the 
    				// index in the forward direction to get the reverse letters?...
    				
    				reverse_array[i] = subStr[i].substring(subStr.length-1);		
    			
    			}
    				
    				
    		
    		
    		}
    		
    		*/
    
        }
    	
    }
    Here's my output:

    Code:
    This
    is
    a
    test
    NOW ENTERING REVERSE SECTION: 
    Current string: This
    Current character: This
    Length = 4
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
    Current string: This
    Current character: is
    Length = 4
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
    Current string: This
    Current character: a
    Length = 4
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
    Current string: This
    Current character: test
    Length = 4
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
    Current string: is
    Current character: This
    Length = 2
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
    Current string: is
    Current character: is
    Length = 2
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
    Current string: a
    Current character: This
    Length = 1
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
    Current string: test
    Current character: This
    Length = 4
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
    Current string: test
    Current character: is
    Length = 4
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
    Current string: test
    Current character: a
    Length = 4
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
    Current string: test
    Current character: test
    Length = 4
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a test
  26. #29
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    split("[ ]", 3);
    The 3 says to split the String into 3 pieces.

    'a test' is considered to be one element, but it should have been split into two parts.
    You have asked for 3 elements: This, is, a test
    If you want ALL of the tokens split into separate Strings don't use the 3.
  28. #30
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    I got it, so yeah, like you said, why use 3 when this has to be modeled for any paragraph in terms of reversing any example I give the program.

    So instead of 3, I used count within the .split() method, and the results are great.

    I took out some unnecessary for loops, and I know now where the reversal code has to go, aka the last for loop at the bottom where I successfully print out each element of the parts_of_paragraph array.

    I don't want to make another embedded for loop again, because we all know that causes problems in most cases for me... so let's see.

    In order to reverse the given characters of a word, I would have to maybe make the index values of a given character for a word to be negative, so I could place it in the corresponding character position in the opposite direction.

    The comment at the bottom for the reverse section is where I'm debating the above logic, here's my code:
    Code:
    import java.sql.Date;
    
    public class Chapt4_Exercise5 
    {
    	
    	public static void main (String[] args)
    	
    	{
    		/* 5. Write a program that reverses the sequence of letters in each 
    		 * word of your chosen paragraph from Exercise 3. 
    		 * 
    		 * For instance, "To be or not to be." becomes "oT eb ro ton ot eb." 
    		 */
    		
    		// Old Paragraph String:
    		// String paragraph = "Far best is he who knows all things himself. Good, he that hearkens when men counsel right. But he who neither knows, nor lays to heart. Another's wisdom, is a useless wight.";	
    		
    		String paragraph = "This is a test";
    		
    		// Declare integer variable for length:
    		int length;
    	
    		// 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); 
    			
    		} 
    		
    		
    		// REVERSE SECTION:
    		
    		System.out.println("NOW ENTERING REVERSE SECTION: ");
    		
    		// Make a String array that will hold the reverse forms of the
    		// elements found in the subStr[] array:
    		String[] reverse_array = new String[count]; 
    
    			
    		// Using A Tokenizer is on p. 149:
    		String [] parts_of_paragraph = paragraph.split("[ ]", count);
    			
    		// Print out the parts of the parts_of_paragraph array for debugging:
    		for(int i = 0; i < parts_of_paragraph.length; i++)
    		{
    			// Print out element being examined:
    			System.out.println("Current element of parts: " + parts_of_paragraph[i]);
    				
    			// Reverse code goes in here:
    			
    		}
    				
    				
    		}
    		
    
        }

    Output:

    Code:
    This
    is
    a
    test
    NOW ENTERING REVERSE SECTION: 
    Current element of parts: This
    Current element of parts: is
    Current element of parts: a
    Current element of parts: test

IMN logo majestic logo threadwatch logo seochat tools logo