Page 1 of 4 123 ... Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2

    Errors In Reversing Characters in a SubString Array


    Hello,

    So I'm attempting the final exercise in the Ivor Horton book on Beginner Java, and it wants me to take a phrase, and reverse the words.

    So I put the words into separate substring elements inside the subStr[] array, and it worked out fine.

    What I'm trying to do now is work out the logic in how to reverse the letters. I'm trying the same approach for what I did to actually get the substring elements, aka get a do while loop to get the count of characters, and then a later for loop that examines the entire array (not single string this time) and looks at each individual word. Then at that point, I want to look at the current word, then add the end characters to the beginning of the current element of the reverse_array (aka I want to reverse the current word inside the subStr array).

    The problem is coming up with the math on how to tell the program to look at that last character of the word and to place it inside the current reverse_array element.


    Here's my code:
    Code:
    import javax.swing.plaf.basic.BasicBorders.ToggleButtonBorder;
    
    
    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." 
    		 */
    		
    		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.";	
    		
    		// 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); 
    			
    		} 
    		
    		// Make a String array that will hold the reverse forms of the
    		// elements found in the subStr[] array:
    		String[] reverse_array = new String[count]; 
    		
    		// Do the same indexing with a do-while loop to determine
    		// the count, but now for SUBSTR instead of the paragraph:
    		index = 0;
    		
    		// Create a do while loop that will examine the entire substring
    		// array:
    		do 
    		{ 
    			// Increment the count and index values:
    			// In order to obtain the current element of the substring
    			// array, we somehow to specify the current element that
    			// we are examining, even though this is a do-while loop, use
    			// the typical int i variable found in for loops:
    			int i = 0;
    			i++;
    			++count; 
    			++index; 
    			// Assign the current index to the next word inside the paragraph:
    			index = subStr[i].indexOf(separator, index);
    			
    		} while (index != -1); 
    				
    		// Create integer variables for the start index and the end index 
    		// of the subStr String array[]:
    		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 (int 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-index);		
    			
    			}
    				
    				
    		
    		
    		}
    
        }
    	
    }

    Here's the errors I'm receiving as well as some output:

    Code:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -29
    	at java.lang.String.substring(Unknown Source)
    	at Chapt4_Exercise5.main(Chapt4_Exercise5.java:145)
    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.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -29
    at java.lang.String.substring(Unknown Source)
    at Chapt4_Exercise5.main(Chapt4_Exercise5.java:145)
    At line 145 the code used an index that was not within the String.
    Check the logic where the index is computed and make sure it generates an index in the range 0 to the String's length-1

    how to tell the program to look at that last character of the word
    The index for the last character would be the String's length-1.

    A suggestion: Start with a short String, say with 3 words that will make it easier to debug.

    You need to do some debugging. Add some println() statements to the code that print out the values of the variables and their values are changed and as they are used in the logic. The print out will help you see and understand what the code is doing when it executes.
    Last edited by NormR; November 2nd, 2013 at 08:49 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Yeah, good idea about blocking off and just debug it.

    So I blocked off the bottom portion with the comment operator /*, and put print statements for the do-while loop, but its giving me a count of 5 and an index of 1, as well as an i value of 1, ONCE.

    So it only went through the do-while loop once. I can't understand why it only did it once... Hmm..

    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:
    		
    		// Make a String array that will hold the reverse forms of the
    		// elements found in the subStr[] array:
    		String[] reverse_array = new String[count]; 
    		
    		// Do the same indexing with a do-while loop to determine
    		// the count, but now for SUBSTR instead of the paragraph:
    		index = 0;
    		
    		// Declare int i outside of the do while loop so it can change
    		// throughout the course of the loop:
    		int i = 0;
    		
    		// Create a do while loop that will examine the entire substring
    		// array:
    		do
    		{
    		
    			// Hmm, only post-incrementation would make sense to me in this
    			// case, because we want it to count the element after
    			// they've been examined... Why am I receiving a count of 5
    			// even through there's only 4 words in: This Is A Test... strange
    			
    			// And why is this only going through this do-while loop once?
    			i++;
    	
    			count++; 
    			index++; 
    			
    			// Make print statements to debug:
    			System.out.println("Current value of count: " + count);
    
    			System.out.println("Current index value: " + index);
    			
    			System.out.println("Current value of i: " + i);
    			
    			// Assign the current index to the next word inside the paragraph:
    			index = subStr[i].indexOf(separator, index);
    			
    		} while (index != -1); 
    				
    		// 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 is the output I'm talking about:
    Code:
    This
    is
    a
    test
    Current value of count: 5
    Current index value: 1
    Current value of i: 1
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    why it only did it once.
    do{] loops will always execute once. It will repeat the loop only if the condition is true.
    What was the value of the condition in the while()? In other words what was in index?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    The answer to your question: um, an integer value?

    Judging by how I'm assigning the index value, its going to be assigned to the integer value of the elemental position of the next character after a space inside the subStr array.

    After adding in another print statement to see what the current element being examined actually is, and it stops at "is"... which is blowing my mind...

    Why is it arbitrarily stopping at the end of the second word?

    The index still isn't negative at that point.

    I guess I'm not that good at do-while loops yet...

    Here's my code with that additional print statement:
    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:
    		
    		// Make a String array that will hold the reverse forms of the
    		// elements found in the subStr[] array:
    		String[] reverse_array = new String[count]; 
    		
    		// Do the same indexing with a do-while loop to determine
    		// the count, but now for SUBSTR instead of the paragraph:
    		index = 0;
    		
    		// Declare int i outside of the do while loop so it can change
    		// throughout the course of the loop:
    		int i = 0;
    		
    		// Create a do while loop that will examine the entire substring
    		// array:
    		
    		// DEBUGGING SECTION:
    		do
    		{
    		
    			// And why is this only going through this do-while loop once?
    			i++;
    			count++; 
    			index++; 
    			
    			// Make print statements to debug:
    			System.out.println("i = " + i);
    			System.out.println("index = " + index);
    			System.out.println("count = " + count);
    			System.out.println("element = " + subStr[i]);
    
    			
    			// Assign the current index to the next word inside the paragraph:
    			index = subStr[i].indexOf(separator, index);
    			
    		} while (index != -1); 
    				
    		// 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);		
    			
    			}
    				
    				
    		
    		
    		}
    		
    		*/
    
        }
    	
    }

    My output is the following:
    Code:
    This
    is
    a
    test
    i = 1
    index = 1
    count = 5
    element = is
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Code:
    			// Assign the current index to the next word inside the paragraph:
    			index = subStr[i].indexOf(separator, index);
    			
    		} while (index != -1);   //  WHAT IS INDEX HERE ???
    You need to add a call to println after the assignment statement changes the value of index so you can see what value index has when the while() statement executes.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Alright, I added a println statement after the assignment for index as well as one OUTSIDE of the do-while loop, and its negative, which makes sense why it exited the loop (aka it did what I asked), but why is it just stopping at 'is' though?

    There's nothing that's making it stop after the second SubStr element is examined.

    The index shouldn't be negative, its not done looking at the entire subStr array. This seriously has be going "WHAT THE ****" right now.

    Why wouldn't the index number be assigned to the 'a' character in the next word?

    I'm still a bit confused for some odd reason.

    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:
    		
    		// Make a String array that will hold the reverse forms of the
    		// elements found in the subStr[] array:
    		String[] reverse_array = new String[count]; 
    		
    		// Do the same indexing with a do-while loop to determine
    		// the count, but now for SUBSTR instead of the paragraph:
    		index = 0;
    		
    		// Declare int i outside of the do while loop so it can change
    		// throughout the course of the loop:
    		int i = 0;
    		
    		// Create a do while loop that will examine the entire substring
    		// array:
    		
    		// DEBUGGING SECTION:
    		do
    		{
    		
    			// And why is this only going through this do-while loop once?
    			i++;
    			count++; 
    			index++; 
    			
    			// Make print statements to debug:
    			System.out.println("i = " + i);
    			System.out.println("index = " + index);
    			System.out.println("count = " + count);
    			System.out.println("element = " + subStr[i]);
    
    			
    			// Assign the current index to the next word inside the paragraph:
    			index = subStr[i].indexOf(separator, index);
    			
    			System.out.println("index AFTER assignment = " + index);
    			
    		} while (index != -1); 
    		
    		System.out.println("index outside do-while loop: " + index);
    				
    		// 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);		
    			
    			}
    				
    				
    		
    		
    		}
    		
    		*/
    
        }
    	
    }

    Output statement:
    Code:
    This
    is
    a
    test
    i = 1
    index = 1
    count = 5
    element = is
    index AFTER assignment = -1
    index outside do-while loop: -1
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Did you read the API doc for the indexOf() method to see what it means when a -1 is returned?

    stop after the second SubStr element
    What is there about the second element: "is" that should cause it to stop?
    Last edited by NormR; November 5th, 2013 at 02:00 PM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by NormR
    Did you read the API doc for the indexOf() method to see what it means when a -1 is returned?


    What is there about the second element: "is" that should cause it to stop?
    Based on Java Oracle's site:

    .indexOf(): returns the index within this string of the first occurrence of the specified character OR -1 if the character does not occur.

    So, if I'm telling the .indexOf function to look for another space, and assign it to the next index value, then how did it NOT find a space between 'is' and 'a'.

    Hmm, the only major flaw I see is with the second argument for .indexOf(separator, index)

    By specifying the index value from the last iteration of the loop, it probably just stops there, and doesn't reassign itself to another index value.

    Hence, there's a huge flaw in how I wrote that second argument.

    Index should be assigned to the fourth character (programming counting wise from 0 onward) aka the first occurrence of a space in "This is a test", and should be progressively be reassigned to each successive space, so I can determine the amount of words so I can just reverse them later somehow.

    I know you're really showing me the problem so I'm sorry for beating around the bush, and I know it HAS to do with this .indexOf() method, but the only thing I'm thinking where I could be wrong in this, is in that second argument, that's GOTTA be it!
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    how did it NOT find a space between 'is' and 'a'.
    Do you know what String was being searched? For debugging the code should print out the String that was being searched, the String that was being searched for and the value returned by the indexOf() method. You need all that data to evaluate what the code is doing.

    Try writing a very small program with a String and the indexOf() method and print out what the values returned by the indexOf() method for lots of different Strings to see what the method does. The code should be less than 5 lines (minus main() and class)
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by NormR
    Do you know what String was being searched? For debugging the code should print out the String that was being searched, the String that was being searched for and the value returned by the indexOf() method. You need all that data to evaluate what the code is doing.

    Try writing a very small program with a String and the indexOf() method and print out what the values returned by the indexOf() method for lots of different Strings to see what the method does. The code should be less than 5 lines (minus main() and class)
    True, printing out the string that's being examined would help too.

    Great idea!

    Alright, I'm back after thinking this one over again. Just like the dates program you helped me with so far, this one is in a similar fashion.

    So basically, I successfully made an array that basically copies a given phrase into an overarching array, that contains individual strings that contain each word.

    Great, now to access these words in order to manipulate them, I have to think about this in terms of their dimensions. So in order to really print the given string, I can't just print the entirety of the subStr array, I have to go further into its second dimension to access a given word.

    SO, I made two overarching for loops, one that will look at the entire subStr array with subIndex (confusing variable name, I know, I'll change it if you think of a better one), and the second one will look at a given character within a given string inside of the array with charInSubIndex.

    I added a print statement to look at each string like you mentioned, and the behavior that I wrote out is coming out a bit weird.

    I understand that it should count literally 14 characters, but this do while loop stops at 15 characters.

    Also, the index values aren't really making much sense. It goes throughout "This is a test" once, then looks at "This" twice, and then just finishes off with " is a test" in separate output statements.

    The least I could say at this point is that I'm confused lol. What am I NOT seeing? There has to be some obvious flaw.

    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:
    		
    		// 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++)
    			{
    		
    		
    				// Do the same indexing with a do-while loop to determine
    				// the count, but now for SUBSTR instead of the paragraph:
    				index = 0;
    		
    				// Declare int i outside of the do while loop so it can change
    				// throughout the course of the loop:
    				int i = 0;
    		
    				// Create a do while loop that will examine the entire substring
    				// array:
    		
    				// DEBUGGING SECTION:
    				do
    				{
    		
    					// And why is this only going through this do-while loop once?
    					i++;
    					count++; 
    					index++; 
    			
    					// Print out the string being searched:
    					// System.out.println("String = " subStr)
    			
    					// Make print statements to debug:
    					System.out.println("element = " + subStr[charInSubIndex]);
    					System.out.println("subIndex = " + subIndex);
    					System.out.println("index = " + index);
    					System.out.println("count = " + count);
    					
    
    					// Assign the current index to the next word inside the paragraph:
    					index = subStr[i].indexOf(separator, index);
    			
    					System.out.println("index AFTER assignment = " + index);
    			
    				} while (index != -1); 
    		
    				System.out.println("index outside do-while loop: " + index);
    		}
    		
    		}
    				
    		// 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);		
    			
    			}
    				
    				
    		
    		
    		}
    		
    		*/
    
        }
    	
    }
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    What is the code currently doing? Copy the program's output and paste it here and add some comments saying what is wrong and show what the output should be.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Code:
    This
    is
    a
    test
    NOW ENTERING REVERSE SECTION: 
    element = This
    subIndex = 0
    index = 1
    count = 5
    index AFTER assignment = -1
    index outside do-while loop: -1
    element = is
    subIndex = 0
    index = 1
    count = 6
    index AFTER assignment = -1
    index outside do-while loop: -1
    element = a
    subIndex = 0
    index = 1
    count = 7
    index AFTER assignment = -1
    index outside do-while loop: -1
    element = test
    subIndex = 0
    index = 1
    count = 8
    index AFTER assignment = -1
    index outside do-while loop: -1
    element = This
    subIndex = 1
    index = 1
    count = 9
    index AFTER assignment = -1
    index outside do-while loop: -1
    element = is
    subIndex = 1
    index = 1
    count = 10
    index AFTER assignment = -1
    index outside do-while loop: -1
    element = This
    subIndex = 2
    index = 1
    count = 11
    index AFTER assignment = -1
    index outside do-while loop: -1
    element = This
    subIndex = 3
    index = 1
    count = 12
    index AFTER assignment = -1
    index outside do-while loop: -1
    element = is
    subIndex = 3
    index = 1
    count = 13
    index AFTER assignment = -1
    index outside do-while loop: -1
    element = a
    subIndex = 3
    index = 1
    count = 14
    index AFTER assignment = -1
    index outside do-while loop: -1
    element = test
    subIndex = 3
    index = 1
    count = 15
    index AFTER assignment = -1
    index outside do-while loop: -1
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Was the output what you expected?
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    I don't know what to expect. This do-while loop confuses me.

    Of course, the only reason I'm using one, is the fact that a similar example in the book did the same thing to count individual words.

    I know its good to take the count though for the amount of words so I can declare another array that will contain the reverse characters.

    Hmm, what is the purpose of using i since I'm already at the character level with charInSubIndex that acts like a typical loop variable that cycles through?

    Should I just remove i?

    I know count is used to literally count the amount of characters, index gets assigned to another space.

    So, I removed i since it looks like its useless in this situation and made the index be assigned to the next element where there's a space, by taking in charInSubIndex as the element, and the space separator as well as the current index value as the arguments to .indexOf().

    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++)
    			{
    		
    		
    				// Do the same indexing with a do-while loop to determine
    				// the count, but now for SUBSTR instead of the paragraph:
    				index = 0;
    	
    		
    				// Create a do while loop that will examine the entire substring
    				// array:
    		
    				// DEBUGGING SECTION:
    				do
    				{
    		
    					count++; 
    					index++; 
    			
    					// Print out the string being searched:
    					// System.out.println("String = " subStr)
    			
    					// Make print statements to debug:
    					System.out.println("element = " + subStr[charInSubIndex]);
    					System.out.println("subIndex = " + subIndex);
    					System.out.println("index = " + index);
    					System.out.println("count = " + count);
    					
    
    					// Assign the current index to the next word inside the paragraph:
    					index = subStr[charInSubIndex].indexOf(separator, index);
    			
    					System.out.println("index AFTER assignment = " + index);
    			
    				} while (index != -1); 
    		
    				System.out.println("index outside do-while loop: " + index);
    		}
    		
    		}
    				
    		// 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);		
    			
    			}
    				
    				
    		
    		
    		}
    		
    		*/
    
        }
    	
    }
Page 1 of 4 123 ... Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo