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

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Alright, I was thinking maybe to reverse the loop conditions.

    I was thinking to make j = the length of the string aka 7 in this case, and make sure its greater than 0, and decrement j each time through the loop.

    That way I could add the 7th character, then the 6th character, etc until it reached the end of the string.

    EDIT:
    OH, yes, by making j = the entire length of the word itself, it is a value that is 1 greater than the entire length, so I changed the condition to be the j = the length of the word itself - 1 (in actual programming language of course)

    Why is it not adding the 'T' in Timothy though?

    Hmm, probably has to do with the < 0 condition in the for loop.

    EDIT # 2: I changed the loop condition to be >= 0, and it DID add the final 'T', now to add more words to see if this can work on any set of names.

    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 = "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);
    		
    		// Create a StringBuilder for later use:
    		StringBuilder reverse_string = new StringBuilder();
    			
    		// 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 = ((parts_of_paragraph[i].length())-1); j >= 0; 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));
    				
    				// 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: y
    Here are the contents of the reverse_string StringBuilder: y
    Current char of Timothy: h
    Here are the contents of the reverse_string StringBuilder: yh
    Current char of Timothy: t
    Here are the contents of the reverse_string StringBuilder: yht
    Current char of Timothy: o
    Here are the contents of the reverse_string StringBuilder: yhto
    Current char of Timothy: m
    Here are the contents of the reverse_string StringBuilder: yhtom
    Current char of Timothy: i
    Here are the contents of the reverse_string StringBuilder: yhtomi
    Current char of Timothy: T
    Here are the contents of the reverse_string StringBuilder: yhtomiT
    Last edited by HauntJemimah; December 1st, 2013 at 10:57 AM.
  2. #47
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    So I'm trying to add a space between words once j == 0 at the end of the final loop when its done examining a given word.

    It does work if I add another name after 'Timothy', like 'Samuel' in this case, but it doesn't add a space, which is obviously what should happen in this case.

    Since its a StringBuilder object, could I add an if statement that will append a space at the end once j == 0?

    I have the suggested if loop in quotes, because when I added it, nothing appeared in my output.

    Here is my code with the suggested if loop in quotes at line 141:
    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 Samuel";
    		
    		// 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);
    		
    		// Create a StringBuilder for later use:
    		StringBuilder reverse_string = new StringBuilder();
    			
    		// 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 = ((parts_of_paragraph[i].length())-1); j >= 0; 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));
    				
    				// 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);
    				
    				/* 
    				if (j == 0)
    				{
    					reverse_string.append(" ");
    					
    				}
    				*/
    				
    				
    			}
    			
    		}
    				
    				
    	}
    		
    
    }

    Here's the output:

    Code:
    Timothy
    Samuel
    NOW ENTERING REVERSE SECTION: 
    Current element: Timothy
    Current char of Timothy: y
    Here are the contents of the reverse_string StringBuilder: y
    Current char of Timothy: h
    Here are the contents of the reverse_string StringBuilder: yh
    Current char of Timothy: t
    Here are the contents of the reverse_string StringBuilder: yht
    Current char of Timothy: o
    Here are the contents of the reverse_string StringBuilder: yhto
    Current char of Timothy: m
    Here are the contents of the reverse_string StringBuilder: yhtom
    Current char of Timothy: i
    Here are the contents of the reverse_string StringBuilder: yhtomi
    Current char of Timothy: T
    Here are the contents of the reverse_string StringBuilder: yhtomiT
    Current element: Samuel
    Current char of Samuel: l
    Here are the contents of the reverse_string StringBuilder: yhtomiTl
    Current char of Samuel: e
    Here are the contents of the reverse_string StringBuilder: yhtomiTle
    Current char of Samuel: u
    Here are the contents of the reverse_string StringBuilder: yhtomiTleu
    Current char of Samuel: m
    Here are the contents of the reverse_string StringBuilder: yhtomiTleum
    Current char of Samuel: a
    Here are the contents of the reverse_string StringBuilder: yhtomiTleuma
    Current char of Samuel: S
    Here are the contents of the reverse_string StringBuilder: yhtomiTleumaS
  4. #48
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    add an if statement that will append a space at the end once j == 0?
    That seems like a simple if statement to write. What problems are you having with it?
  6. #49
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    If I add the if statement, no output occurs at all

    EDIT:... Woah

    Yesterday, when I hit the run button, no output occurred. I must of not really pressed it and assumed it ran I guess, spooky.

    It works fine!

    I added the real paragraph and it works great!

    Program is finally done! Thanks a bunch, Norm!

    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 = "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.";
    		
    		System.out.println("Normal String: " + paragraph);
    		
    		// 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);
    		
    		// Create a StringBuilder for later use:
    		StringBuilder reverse_string = new StringBuilder();
    			
    		// 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 = ((parts_of_paragraph[i].length())-1); j >= 0; 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));
    				
    				// 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);
    				
    				if (j == 0)
    				{
    					reverse_string.append(" ");
    					
    				}
    				
    				
    			}
    			
    		}
    			
    		System.out.println("Reverse form of paragraph: " + reverse_string);
    				
    	}
    		
    
    }

    Output:
    Code:
    Normal String: 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.
    Reverse form of paragraph: raF tseb si eh ohw swonk lla sgniht .flesmih ,dooG eh taht snekraeh nehw nem lesnuoc .thgir tuB eh ohw rehtien ,swonk ron syal ot .traeh s'rehtonA ,modsiw si a sselesu .thgiw
Page 4 of 4 First ... 234
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo