#31
  1. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    A simple approach to getting a String with the letters in reverse order:
    Given "ABC"
    loop
    get next letter: "A", "B", "C"
    add letter to start of output string: "A", "BA", "CBA"
    end loop
    The end comments separated by commas are the result each loop
  2. #32
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Alright, I wrote out the notes of the steps you told me, and I got the program to print out individual characters using char at.

    Now on to the next step to add the "j+1" character in the sequence to the current character and print it out to be sure its doing it correctly.

    Here's my code, awesome steps by the way, Norm! :

    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:
    			/*	Notes from NormR
    			 * Given "ABC"
    			 * loop
    			 * get next letter: "A", "B", "C"
    			 * add letter to start of output string: "A", "BA", "CBA"
    			 * end loop
    			 * The end comments separated by commas are the result each loop
    			 * 
    			 */
    			
    			// First step: Loop to look at each individual letter:
    			for(int j = 0; j < parts_of_paragraph[i].length(); j++)
    			{
    				// Output character to be sure you're now in a deeper
    				// level:
    				System.out.println("Current char of " + parts_of_paragraph[i] + ":" 
    						+ parts_of_paragraph[i].charAt(j));
    				
    			}
    			
    		}
    				
    				
    	}
    		
    
    }
  4. #33
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    I'm trying to construct that idea that will extract the next character as well as the current character and add it to a string called reverse_string.

    How would I type cast it to make it to become an overall string?

    Here's the line in question I'm trying to add that obviously has errors that are screaming at me that I can't convert an int to a string:

    Code:
    // Get the next letter and add it to the output string
    				String reverse_string = parts_of_paragraph[i].charAt(j+1) + parts_of_paragraph[i].charAt(j);
    My Code thus far INCLUDING this idea in that embedded for loop:
    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:
    			/*	Notes from NormR
    			 * Given "ABC"
    			 * loop
    			 * get next letter: "A", "B", "C"
    			 * add letter to start of output string: "A", "BA", "CBA"
    			 * end loop
    			 * The end comments separated by commas are the result each loop
    			 * 
    			 */
    			
    			// First step: Loop to look at each individual letter:
    			for(int j = 0; j < parts_of_paragraph[i].length(); j++)
    			{
    				// Output character to be sure you're now in a deeper
    				// level:
    				System.out.println("Current char of " + parts_of_paragraph[i] + ":" 
    						+ parts_of_paragraph[i].charAt(j));
    				
    				// Get the next letter and add it to the output string
    				String reverse_string = parts_of_paragraph[i].charAt(j+1) + parts_of_paragraph[i].charAt(j);
    				
    				
    			}
    			
    		}
    				
    				
    	}
    		
    
    }
  6. #34
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Instead of creating a new String every iteration, use the StringBuilder class to build the contents of the String char by char with the append() and insert() methods. When all the chars have been added have the StringBuilder object create a String from its contents.
  8. #35
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    So I created a StringBuilder called reverse_string.

    I did append the current character to it each time through the loop.

    I'm trying to figure out the .insert() method you mentioned that is used with the StringBuilder class.

    Is it assumed that I have to create a CharSequence aka an n+1 type sequence BEFORE I make it insert the sequence of characters into the StringBuilder object?

    Sorry if I come off as confused, but I tried to read the original docs on it from Sun's main site, and they were a bit confusing.

    This page from TutorialsPoint was a bit more helpful, but I'm unsure if I should use a CharSequence at this point.

    Here's the link I'm talking about:
    http://www.tutorialspoint.com/java/lang/java_lang_stringbuilder.htm

    Here's my code:
    Code:
    import java.sql.Date;
    
    import org.omg.CORBA.PUBLIC_MEMBER;
    
    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:
    			/*	Notes from NormR
    			 * Given "ABC"
    			 * loop
    			 * get next letter: "A", "B", "C"
    			 * add letter to start of output string: "A", "BA", "CBA"
    			 * end loop
    			 * The end comments separated by commas are the result each loop
    			 * 
    			 */
    			
    			// First step: Loop to look at each individual letter:
    			for(int j = 0; j < parts_of_paragraph[i].length(); j++)
    			{
    				// Output character to be sure you're now in a deeper
    				// level:
    				System.out.println("Current char of " + parts_of_paragraph[i] + ":" 
    						+ parts_of_paragraph[i].charAt(j));
    				
    				// Get the next letter and add it to the output string:
    				StringBuilder reverse_string = new StringBuilder();
    				reverse_string.append(parts_of_paragraph[i].charAt(j));
    				
    				System.out.println("Here are the contents of the reverse_string StringBuilder: " + reverse_string);
    				
    			}
    			
    		}
    				
    				
    	}
    		
    
    }
  10. #36
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What happens when the code is compiled and executed?
  12. #37
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    It appears to run okay. Its printing out each character in each word as it goes through the entire array.

    How would I know if I hit the end of a current word and know how many characters a given word contained?

    I ask because if I knew how many character were in a word, then I could maybe go backwards in a decrementing fashion to add the characters that way to the reverse string.


    Here is the output:
    Code:
    This
    is
    a
    test
    NOW ENTERING REVERSE SECTION: 
    Current element of parts: This
    Current char of This:T
    Here are the contents of the reverse_string StringBuilder: T
    Current char of This:h
    Here are the contents of the reverse_string StringBuilder: h
    Current char of This:i
    Here are the contents of the reverse_string StringBuilder: i
    Current char of This:s
    Here are the contents of the reverse_string StringBuilder: s
    Current element of parts: is
    Current char of is:i
    Here are the contents of the reverse_string StringBuilder: i
    Current char of is:s
    Here are the contents of the reverse_string StringBuilder: s
    Current element of parts: a
    Current char of a:a
    Here are the contents of the reverse_string StringBuilder: a
    Current element of parts: test
    Current char of test:t
    Here are the contents of the reverse_string StringBuilder: t
    Current char of test:e
    Here are the contents of the reverse_string StringBuilder: e
    Current char of test:s
    Here are the contents of the reverse_string StringBuilder: s
    Current char of test:t
    Here are the contents of the reverse_string StringBuilder: t
    Last edited by HauntJemimah; November 29th, 2013 at 06:41 PM.
  14. #38
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    how to make it add the last character in a given word before a space to this accumulating string
    Can you explain and give an example?
  16. #39
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Good point.

    So if I'm looking at a word right that's denoted by the i level,

    and if I know that if a space is a delimiter, I should maybe tokenize the given word to find out how many characters are present BEFORE a space is encountered.

    This way, if I know the total amount of characters, I could make that value negative to make the program index into the word from right to left, and add characters as it goes in that order.

    The problem is how am I going to write it.

    If I take the count of a given word, multiply it by negative 1, and make a for loop or some kind of looping structure that will add 1 to itself to make this value go from negative to positive until it has reached the left side of the given word, THEN I could add characters to an accumulating string as it goes from right to left.

    If you want an example, that's fine too:

    So say, we have a word that's seven characters long like 'Timothy' right?

    I want to somehow count the amount of characters in the given word and store it into an integer variable called char_count

    So:

    int char_count;

    (insert looping structure to actually do the counting)

    * after looping:

    int char_count = 7;

    char_count *= -1;

    Using this char_count variable, I would make a for loop or something that would go as follows:

    for (int k = 0; k < char_count; k--)

    ^ The above for loop is actually totally off, and I'm thinking actually this is going to involve index values instead...

    So as you can see just thinking this out, I'm a bit confused on how to do about making a structure that examines a word from right to left using a negative index value with incrementation.

    I tried using negative j values for that last for loop, and maybe using decrementing with j to make it go backwards for a given word, but then only the words are printed and the for loop is never reached.

    I probably could use built-in string functions for Java to do the same work using negative index values, hmm.
    Last edited by HauntJemimah; November 30th, 2013 at 12:18 PM.
  18. #40
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Given the String: "Timothy" what is the desired results from the code?
    If results is a String that is built a char at a time, show what the contents of results is each time around the loop. Write 7 lines showing in each what is in results at the end of that iteration of the loop.


    a negative index value
    index values range from 0 to the length-1. They are never negative.
  20. #41
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    ^ Are you meaning to say that I should branch this program off as well as the idea into a SEPARATE program, or are you meaning to just simply replace the original string with 'Timothy' instead of 'This is a test'?

    Either way, I replaced 'This is a test' with 'Timothy', and it appears like this string builder string is just being overwritten with a new character each time through the loop and not really 'building' a string, but more or less overwriting itself with a new character.

    I've never used StringBuilder before so this is probably why I can't explain its behavior.

    Anyway, here's the code with the replaced string above,

    and its output:
    Code:
    import java.sql.Date;
    
    import org.omg.CORBA.PUBLIC_MEMBER;
    
    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 = "Timothy";
    		
    		// 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: " + parts_of_paragraph[i]);
    				
    			// Reverse code goes in here:
    			/*	Notes from NormR
    			 * Given "ABC"
    			 * loop
    			 * get next letter: "A", "B", "C"
    			 * add letter to start of output string: "A", "BA", "CBA"
    			 * end loop
    			 * The end comments separated by commas are the result each loop
    			 * 
    			 */
    			
    			// First step: Loop to look at each individual letter:
    			for(int j = 0; j < parts_of_paragraph[i].length(); j++)
    			{
    				// Output character to be sure you're now in a deeper
    				// level:
    				System.out.println("Current char of " + parts_of_paragraph[i] + ": " 
    						+ parts_of_paragraph[i].charAt(j));
    				
    				// Get the next letter and add it to the output string:
    				StringBuilder reverse_string = new StringBuilder();
    				
    				// Confused if I should use .append or .insert, and 
    				// whether or not to add a CharSeq as well:
    				reverse_string.append(parts_of_paragraph[i].charAt(j));
    				
    				System.out.println("Here are the contents of the reverse_string StringBuilder: " + reverse_string);
    				
    			}
    			
    		}
    				
    				
    	}
    		
    
    }
    Code:
    Timothy
    NOW ENTERING REVERSE SECTION: 
    Current element: Timothy
    Current char of Timothy: T
    Here are the contents of the reverse_string StringBuilder: T
    Current char of Timothy: i
    Here are the contents of the reverse_string StringBuilder: i
    Current char of Timothy: m
    Here are the contents of the reverse_string StringBuilder: m
    Current char of Timothy: o
    Here are the contents of the reverse_string StringBuilder: o
    Current char of Timothy: t
    Here are the contents of the reverse_string StringBuilder: t
    Current char of Timothy: h
    Here are the contents of the reverse_string StringBuilder: h
    Current char of Timothy: y
    Here are the contents of the reverse_string StringBuilder: y
  22. #42
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    it appears like this string builder string is just being overwritten with a new character each time through the loop
    The code creates a NEW instance of StringBuilder every time around the loop.
    The version created in the previous time around the loop is replaced. If you want to accumulate values in a StringBuilder then create it outside of the loop.
  24. #43
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Originally Posted by NormR
    Given the String: "Timothy" what is the desired results from the code?
    If results is a String that is built a char at a time, show what the contents of results is each time around the loop. Write 7 lines showing in each what is in results at the end of that iteration of the loop.



    index values range from 0 to the length-1. They are never negative.
    Okay, but can you go from right to left in terms of looking at characters in a string?

    I think in Python, you can do this with negative index values, hence why I'm messing up my thinking.

    Sorry about that, I guess its not in Java then.
  26. #44
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    can you go from right to left
    Yes, for example a String of length 4 has index values that range from 0 to 3 or 3 to 0
    the char at 3 is at the end
    the char at 2 is is one in from the end
    etc
  28. #45
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Originally Posted by NormR
    The code creates a NEW instance of StringBuilder every time around the loop.
    The version created in the previous time around the loop is replaced. If you want to accumulate values in a StringBuilder then create it outside of the loop.
    (Applause icon if available lol)

    That's it!

    Very straightforward, how did I not see that! Good point, I'll try that!

IMN logo majestic logo threadwatch logo seochat tools logo