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

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    why the long running loop? There is only one date with 3 parts.
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by NormR
    why the long running loop? There is only one date with 3 parts.

    That was only when I was assigning the current date to the j element of the array.

    I think it should be only assigned to the k element of the array since k specifies the specific character that I want to examine.

    That's the major problem I'm having, whether to use j or k to perform these operations. I don't mean to ask again, but which one do I use?
  4. #18
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I don't know what data is contained in those variables. Can you rename them to represent what data they are being used to index through?
    There are arrays of dates and Strings and ???
    It looks like i should be named: dateIndex (used through dates in the array
    and j should be named charInDateIndex (used to index through the char in a date)

    but what is k used for?
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by NormR
    I don't know what data is contained in those variables. Can you rename them to represent what data they are being used to index through?
    There are arrays of dates and Strings and ???
    It looks like i should be named: dateIndex (used through dates in the array
    and j should be named charInDateIndex (used to index through the char in a date)

    but what is k used for?
    True, that was my point exactly. I was trying to ask myself, what the **** am I using k for anyway?

    You're right, its just working with 2 dimensions, and that's it.

    So I changed i and j to be date_index and charInDateIndex respectively.

    Now, looking at this, it makes a hell of a lot more sense now.

    The thing is how I'm trying to understand the faults inside how I wrote the do-while loop.

    Its continuing even when an index number is negative aka when all characters have been examined.

    The pattern for the index is as follows for the "5/17/91" date,

    it becomes 1 when it finds the first slash, 4 when it finds the next slash, and -1 when it reaches the end.

    Why won't this loop stop even though I told it to stop when a negative index value was found in the while() section.

    I understand the whole purpose of that suggested do-while loop from my book used in a similar example in the sense that by doing it this way, I can count the amount of words in a given string.

    The act of actually stopping the loop at that point is what is messing me up...

    Here's my code:

    Code:
    public class Chapt4_Exercise4 
    {
    	
    	public static void main(String[] args)
    	{
    		
    		/* 4. Define an array of ten String elements each containing an 
    		 * arbitrary string of the form “month/day/year”; 
    		 * 
    		 * for example, "10/29/99" or "12/5/01". 
    		 * 
    		 * Analyze each element in the array and output the date represented in 
    		 * the form 29th October 1999.
    	     */
    		
    		// change the array to hold one date
    		String [] date = {"5/17/91"};
    		
    		// Old date String that contains real dates:
    		/*
    		String [] date = {"5/17/91", "5/10/93", "6/13/73", "7/26/94",
    							   "6/21/96", "9/3/01", "10/25/05", "12/25/96",
    							   "8/21/93", "4/16/03"};
    	    */
    		
    		
    		
    		// Look at each individual date of the array with a for loop:
    		
    		for(int date_index = 0; date_index < date.length; date_index++)
    		{
    			
    			// Make another for loop to examine the current date string:
    			for(int charInDateIndex = 0; charInDateIndex < date[date_index].length(); charInDateIndex++)
    			{
    				// Extract element # 2 and 3, and add it to a string 
    				// accumulator that will be used to output the date in the
    				// new format:
    				
    				// Specify the delimiters you want to use to separate tokens:
    				char separator = '/'; 
    				
    				int count = 0; 
    				
    				// Make a do while loop to count the number of substrings:
    				
    				// You need to index into the array to get a String object 
    				// using array notation []: aStringArray[ix].indexOf(...
    				
    				int index = 0; 
    				
    				// Break the date string into 3 parts:
    				// First Part: Month 
    				// Second Part: Day 
    				// Third Part: Year 
    				
    				// HERE'S FIRST PART (MONTH):
    				do 
    				{ 
    					++count; 
    					++index; 
    
    					// Assign the current index value to use the .indexOf
    					// method on the current el
    					index = date[charInDateIndex].indexOf(separator, index); 
    					
    					// Make print statements to see what's going on:
    					System.out.println("count = " + count);
    					System.out.println("index = " + index);
    					System.out.println("An ID: " + java.util.Arrays.toString(date));
    
    				} while (index != -1 || charInDateIndex != -1);
    				
    			}
    		}
    	}		
    }
  8. #20
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Code:
    } while (index != -1 || charInDateIndex != -1);
    The loop will continue if EITHER of those conditions are true.
    It will stop if conditions BOTH are false.

    What are the values of those variables at the end of the loop just before the while() statement?
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Ugh, wrong post lol.
    Last edited by HauntJemimah; November 8th, 2013 at 03:11 PM.
  12. #22
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Alright, I'm still confused honestly overall, but I did kind of come up with an if statement that might add the month, when the separator slash has been reached AND the current element is less than 1 aka its going to be the month value for the date.

    How I phrased, is completely wrong, and its either one of the if statement conditions that is off. For some reason, I can't figure out if the current element inside charInDateIndex is either equal to the actual character being examined, or is it a number that represents the element being examined aka it equals 0 for the zeroth element, 1 for the 1st element.

    Anyway, here's what I have, and I did remove the second condition in the while statement so it reached the end of the date successfully at least.

    Here's my code:
    Code:
    public class Chapt4_Exercise4 
    {
    	
    	public static void main(String[] args)
    	{
    		
    		/* 4. Define an array of ten String elements each containing an 
    		 * arbitrary string of the form “month/day/year”; 
    		 * 
    		 * for example, "10/29/99" or "12/5/01". 
    		 * 
    		 * Analyze each element in the array and output the date represented in 
    		 * the form 29th October 1999.
    	     */
    		
    		// change the array to hold one date
    		String [] date = {"5/17/91"};
    		
    		// Old date String that contains real dates:
    		/*
    		String [] date = {"5/17/91", "5/10/93", "6/13/73", "7/26/94",
    							   "6/21/96", "9/3/01", "10/25/05", "12/25/96",
    							   "8/21/93", "4/16/03"};
    	    */
    		
    		
    		
    		// Look at each individual date of the array with a for loop:
    		
    		// Declare an integer to store the month:
    		int month = 1;
    		
    		// Declare an integer to store the day:
    		int day = 1;
    		
    		// Declare an integer to store the year:
    		int year = 1;
    		
    		for(int date_index = 0; date_index < date.length; date_index++)
    		{
    			
    			// Make another for loop to examine the current date string:
    			for(int charInDateIndex = 0; charInDateIndex < date[date_index].length(); charInDateIndex++)
    			{
    				// Extract element # 2 and 3, and add it to a string 
    				// accumulator that will be used to output the date in the
    				// new format:
    				
    				// Specify the delimiters you want to use to separate tokens:
    				char separator = '/'; 
    				
    				// Make an integer variable to count each character:
    				int count = 0; 
    				
    				// You need to index into the array to get a String object 
    				// using array notation []: aStringArray[ix].indexOf(...
    				
    				// Set the index to be zero to look at the first character
    				int index = 0; 
    				
    				// Break the date string into 3 parts:
    				// Month Day Year 
    				
    				// HERE'S FIRST PART (MONTH):
    				do 
    				{ 
    					++count; 
    					++index; 
    					
    					// Add an if statement that will look to see if
    					// the current charInDateIndex element is a slash
    					// AND its less than 1, IF SO, add this to the month:
    					if(charInDateIndex == separator && charInDateIndex < 1)
    					{
    						// Add the current element to the month integer variable:
    						
    						
    					}
    
    					// Assign the current index value to use the .indexOf
    					// method on the current el
    					index = date[charInDateIndex].indexOf(separator, index); 
    					
    					// Make print statements to see what's going on:
    					System.out.println("count = " + count);
    					System.out.println("index = " + index);
    					System.out.println("An ID: " + java.util.Arrays.toString(date));
    
    				} while (index != -1);
    				
    			}
    		}
    	}		
    }

    Here's my output:
    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at Chapt4_Exercise4.main(Chapt4_Exercise4.java:64)
    count = 1
    index = 1
    An ID: [5/17/91]
    count = 2
    index = 4
    An ID: [5/17/91]
    count = 3
    index = -1
    An ID: [5/17/91]
  14. #23
  15. 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.ArrayIndexOutOfBoundsException: 1
    at Chapt4_Exercise4.main(Chapt4_Exercise4.java:64)
    At line 64 the code uses an index past the end of the array. The array must have less than 2 elements: an index of 1 is out of bounds.
  16. #24
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by NormR
    At line 64 the code uses an index past the end of the array. The array must have less than 2 elements: an index of 1 is out of bounds.

    What if I add an if statement to prevent it from changing the index value when its positive?

    Or should I change the while section of the loop?

    I think the problem is that since I'm mixing the for loop with the do-while loop, the for loop is making the do while loop repeat itself, even though its a loop designed to be run only once.

    So, ditch this confusing ****ing do-while loop altogether then? lol, I just wish I knew what I was doing.

    Anyway, here's my code with that if statement I was talking about:
    Code:
    public class Chapt4_Exercise4 
    {
    	
    	public static void main(String[] args)
    	{
    		
    		/* 4. Define an array of ten String elements each containing an 
    		 * arbitrary string of the form “month/day/year”; 
    		 * 
    		 * for example, "10/29/99" or "12/5/01". 
    		 * 
    		 * Analyze each element in the array and output the date represented in 
    		 * the form 29th October 1999.
    	     */
    		
    		// change the array to hold one date
    		String [] date = {"5/17/91"};
    		
    		// Old date String that contains real dates:
    		/*
    		String [] date = {"5/17/91", "5/10/93", "6/13/73", "7/26/94",
    							   "6/21/96", "9/3/01", "10/25/05", "12/25/96",
    							   "8/21/93", "4/16/03"};
    	    */
    		
    		
    		
    		// Look at each individual date of the array with a for loop:
    		
    		// Declare an integer to store the month:
    		int month = 1;
    		
    		// Declare an integer to store the day:
    		int day = 1;
    		
    		// Declare an integer to store the year:
    		int year = 1;
    		
    		for(int date_index = 0; date_index < date.length; date_index++)
    		{
    			
    			// Make another for loop to examine the current date string:
    			for(int charInDateIndex = 0; charInDateIndex < date[date_index].length(); charInDateIndex++)
    			{
    				// Extract element # 2 and 3, and add it to a string 
    				// accumulator that will be used to output the date in the
    				// new format:
    				
    				// Specify the delimiters you want to use to separate tokens:
    				char separator = '/'; 
    				
    				// Make an integer variable to count each character:
    				int count = 0; 
    				
    				// You need to index into the array to get a String object 
    				// using array notation []: aStringArray[ix].indexOf(...
    				
    				// Set the index to be zero to look at the first character
    				int index = 0; 
    				
    				// Break the date string into 3 parts:
    				// Month Day Year 
    				
    				// HERE'S FIRST PART (MONTH):
    				do 
    				{ 
    					++count; 
    					++index; 
    					
    					// Add an if statement that will look to see if
    					// the current charInDateIndex element is a slash
    					// AND its less than 1, IF SO, add this to the month:
    					if(charInDateIndex == separator && charInDateIndex < 1)
    					{
    						// Add the current element to the month integer variable:
    						
    						
    					}
    
    					// Assign the current index value to use the .indexOf
    					// method on the current el
    					
    					// Make an if statement that will assign the next
    					// index to the next space AS LONG AS INDEX IS POSITIVE:
    					
    					if(index > 1)
    					{
    						index = date[charInDateIndex].indexOf(separator, index);
    					}
    					
    					// Make print statements to see what's going on:
    					System.out.println("count = " + count);
    					System.out.println("index = " + index);
    					System.out.println("An ID: " + java.util.Arrays.toString(date));
    
    				} while (index > -1);
    				
    			}
    		}
    	}		
    }

    Output, same exception error:
    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at Chapt4_Exercise4.main(Chapt4_Exercise4.java:89)
    count = 1
    index = 1
    An ID: [5/17/91]
    count = 2
    index = 2
    An ID: [5/17/91]
    count = 3
    index = 4
    An ID: [5/17/91]
    count = 4
    index = -1
    An ID: [5/17/91]
    count = 1
    index = 1
    An ID: [5/17/91]
    count = 2
    index = 2
    An ID: [5/17/91]
  18. #25
  19. 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.ArrayIndexOutOfBoundsException: 1
    at Chapt4_Exercise4.main(Chapt4_Exercise4.java:89)
    Looks like the same problem as before (post#23)


    What index is supposed to be used for the date array?
    Last edited by NormR; November 9th, 2013 at 01:48 PM.
  20. #26
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Its supposed to be not negative and move on to the next character?

    The index value becomes negative when it hits the end of the current word or string in that particular array.

    Its not going through each character without jumping to the next index value. So this loop is really dependent on finding the next separator, which is fine, but shouldn't I account the count variable to include the remainder between the current count as well as the current index value?

    Ex: During the second time though the loop, count was equal to 2 which means that the second character of 5/17/91 was being examined aka the slash mark, while index = 4, which means index was equal to the second slash.

    There are two characters that were not accounted for the current count of characters between count and index, so shouldn't I create a remainder variable that subtracts the current difference between index and count, and adds it to count each time through the loop to ensure that I've counted all the characters.

    Or wouldn't count be just dictated by the current elemental character being examined aka charAtIndex.

    The only thing I could think of to force the do-while loop to look at each character was to add another or statement that made sure that count was at least 7 since there are 7 characters in 5/17/91.

    Anyway, obviously as you can see by my explanation, I have absolutely no ****ing clue on what to do... ah... Anyway, I'm trying.

    So here's my code, hopefully I answered your question:
    Code:
    public class Chapt4_Exercise4 
    {
    	
    	public static void main(String[] args)
    	{
    		
    		/* 4. Define an array of ten String elements each containing an 
    		 * arbitrary string of the form “month/day/year”; 
    		 * 
    		 * for example, "10/29/99" or "12/5/01". 
    		 * 
    		 * Analyze each element in the array and output the date represented in 
    		 * the form 29th October 1999.
    	     */
    		
    		// change the array to hold one date
    		String [] date = {"5/17/91"};
    		
    		// Old date String that contains real dates:
    		/*
    		String [] date = {"5/17/91", "5/10/93", "6/13/73", "7/26/94",
    							   "6/21/96", "9/3/01", "10/25/05", "12/25/96",
    							   "8/21/93", "4/16/03"};
    	    */
    		
    		
    		
    		// Look at each individual date of the array with a for loop:
    		
    		// Declare an integer to store the month:
    		int month = 1;
    		
    		// Declare an integer to store the day:
    		int day = 1;
    		
    		// Declare an integer to store the year:
    		int year = 1;
    		
    		for(int date_index = 0; date_index < date.length; date_index++)
    		{
    			
    			// Make another for loop to examine the current date string:
    			for(int charInDateIndex = 0; charInDateIndex < date[date_index].length(); charInDateIndex++)
    			{
    	
    				
    				// Specify the delimiters you want to use to separate tokens:
    				char separator = '/'; 
    				
    				// Make an integer variable to count each character:
    				int count = 0; 
    				
    				// Set the index to be zero to look at the first character
    				int index = 0; 
    				
    				// Break the date string into 3 parts:
    				// Month Day Year 
    				
    				// HERE'S FIRST PART (MONTH):
    				do 
    				{ 
    					System.out.println("Count BEFORE pre-increment = " + count);
    					System.out.println("Index BEFORE pre-increment = " + index);
    					++count; 
    					++index; 
    					
    					// Add an if statement that will look to see if
    					// the current charInDateIndex element is a slash
    					// AND its less than 1, IF SO, add this to the month:
    					if(charInDateIndex == separator && charInDateIndex < 1)
    					{
    						// Add the current element to the month integer variable:
    						
    						
    					}
    
    					// Assign the current index value to use the .indexOf
    					// method on the current el
    					
    					// Make an if statement that will assign the next
    					// index to the next space AS LONG AS INDEX IS POSITIVE:
    					
    					if(index > 0)
    					{
    						index = date[charInDateIndex].indexOf(separator, index);
    					}
    					
    					// Make print statements to see what's going on:
    					System.out.println("count = " + count);
    					System.out.println("index = " + index);
    					System.out.println("An ID: " + java.util.Arrays.toString(date));
    
    				} while (index > -1 || count < 7);
    				
    			}
    		}
    	}		
    }
    Code:
    Output:
    
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at Chapt4_Exercise4.main(Chapt4_Exercise4.java:86)
    Count BEFORE pre-increment = 0
    Index BEFORE pre-increment = 0
    count = 1
    index = 1
    An ID: [5/17/91]
    Count BEFORE pre-increment = 1
    Index BEFORE pre-increment = 1
    count = 2
    index = 4
    An ID: [5/17/91]
    Count BEFORE pre-increment = 2
    Index BEFORE pre-increment = 4
    count = 3
    index = -1
    An ID: [5/17/91]
    Count BEFORE pre-increment = 3
    Index BEFORE pre-increment = -1
    count = 4
    index = 0
    An ID: [5/17/91]
    Count BEFORE pre-increment = 4
    Index BEFORE pre-increment = 0
    count = 5
    index = 1
    An ID: [5/17/91]
    Count BEFORE pre-increment = 5
    Index BEFORE pre-increment = 1
    count = 6
    index = 4
    An ID: [5/17/91]
    Count BEFORE pre-increment = 6
    Index BEFORE pre-increment = 4
    count = 7
    index = -1
    An ID: [5/17/91]
    Count BEFORE pre-increment = 0
    Index BEFORE pre-increment = 0
  22. #27
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    In post#13 I listed the steps the program could take to solve the problem.
    Which of the steps are working in the present code
    and what step are you currently working on?
  24. #28
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by NormR
    In post#13 I listed the steps the program could take to solve the problem.
    Which of the steps are working in the present code
    and what step are you currently working on?
    Ah yes, three steps, month date and year.

    Pretty amazingly straightforward.

    I'm currently working on extracting the day portion.

    So, I guess I have to work on extracting the 3rd and 4th character of each date.

    Though... my thought is that I cannot just generalize that either though, what if I have a date with 12/17/91 for ex, then the day would then be 4th and 5th element

    Hmm.

    Anyway, I'm trying to add a print statement to get it to print the current character, it does print the ID 7 times meaning that it goes through the for loop 7 times to examine all 7 characters, which is good for once.

    Now its the matter of printing that character. I tried using a System.out.println("Current character = " + date(charInDateIndex)); but that isn't working...

    I tried using that line you used previously just to print the id using the .toString() method from the Arrays class, but that didn't work either.

    Hmm...

    Thanks for bearing with me. That do-while loop really really pissed me off. I think its the fact that I really have to understand the overarching for loops that are going through each character before I even add a do while loop that runs at the same time anyway.


    Here's my code:
    Code:
    public class Chapt4_Exercise4 
    {
    	
    	public static void main(String[] args)
    	{
    		
    		/* 4. Define an array of ten String elements each containing an 
    		 * arbitrary string of the form “month/day/year”; 
    		 * 
    		 * for example, "10/29/99" or "12/5/01". 
    		 * 
    		 * Analyze each element in the array and output the date represented in 
    		 * the form 29th October 1999.
    	     */
    		
    		// change the array to hold one date
    		String [] date = {"5/17/91"};
    		
    		// Old date String that contains real dates:
    		/*
    		String [] date = {"5/17/91", "5/10/93", "6/13/73", "7/26/94",
    							   "6/21/96", "9/3/01", "10/25/05", "12/25/96",
    							   "8/21/93", "4/16/03"};
    	    */
    		
    		
    		
    		// Look at each individual date of the array with a for loop:
    		
    		// Declare an integer to store the month:
    		int month = 1;
    		
    		// Declare an integer to store the day:
    		int day = 1;
    		
    		// Declare an integer to store the year:
    		int year = 1;
    		
    		for(int date_index = 0; date_index < date.length; date_index++)
    		{
    			
    			// Make another for loop to examine the current date string:
    			for(int charInDateIndex = 0; charInDateIndex < date[date_index].length(); charInDateIndex++)
    			{
    	
    				
    				// Specify the delimiters you want to use to separate tokens:
    				char separator = '/'; 
    				
    				// Make an integer variable to count each character:
    				int count = 0; 
    				
    				// Set the index to be zero to look at the first character
    				int index = 0; 
    				
    				// Break the date string into 3 parts:
    				// Month Day Year 
    				
    				// HERE'S FIRST PART (MONTH):
    				
    				System.out.println("An ID: " + java.util.Arrays.toString(date));
    
    				// Print out the current character being examined:
    				System.out.println("Current character = " + date(charInDateIndex));
    				
    			}
    		}
    	}		
    }
  26. #29
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    I tried adding brackets to maybe index into the string array, buts still only giving me the full 'id' without the brackets.

    So should I go a dimension further to get the character or am I indexing the date array's first element incorrectly?

    Here's my code:
    Code:
    public class Chapt4_Exercise4 
    {
    	
    	public static void main(String[] args)
    	{
    		
    		/* 4. Define an array of ten String elements each containing an 
    		 * arbitrary string of the form “month/day/year”; 
    		 * 
    		 * for example, "10/29/99" or "12/5/01". 
    		 * 
    		 * Analyze each element in the array and output the date represented in 
    		 * the form 29th October 1999.
    	     */
    		
    		// change the array to hold one date
    		String [] date = {"5/17/91"};
    		
    		// Old date String that contains real dates:
    		/*
    		String [] date = {"5/17/91", "5/10/93", "6/13/73", "7/26/94",
    							   "6/21/96", "9/3/01", "10/25/05", "12/25/96",
    							   "8/21/93", "4/16/03"};
    	    */
    		
    		// Look at each individual date of the array with a for loop:
    		
    		// Declare an integer to store the month:
    		int month = 1;
    		
    		// Declare an integer to store the day:
    		int day = 1;
    		
    		// Declare an integer to store the year:
    		int year = 1;
    		
    		for(int date_index = 0; date_index < date.length; date_index++)
    		{
    			
    			// Make another for loop to examine the current date string:
    			for(int charInDateIndex = 0; charInDateIndex < date[date_index].length(); charInDateIndex++)
    			{
    	
    				
    				// Specify the delimiters you want to use to separate tokens:
    				char separator = '/'; 
    				
    				// Make an integer variable to count each character:
    				int count = 0; 
    				
    				// Set the index to be zero to look at the first character
    				int index = 0; 
    				
    				// Break the date string into 3 parts:
    				// Month Day Year 
    				
    				// HERE'S FIRST PART (MONTH):
    				System.out.println("An ID: " + java.util.Arrays.toString(date));
    
    				// Print out the current character being examined:
    				System.out.println("Current character = " + date[charInDateIndex]);
    				
    			}
    		}
    	}		
    }

    Output:
    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at Chapt4_Exercise4.main(Chapt4_Exercise4.java:62)
    An ID: [5/17/91]
    Current character = 5/17/91
    An ID: [5/17/91]
  28. #30
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    indexing the date array's first element incorrectly
    To index the first element in an array use an index of 0: theArray[0]

    Current character = 5/17/91
    That message is misleading. It doesn't show a character. Shouldn't it be:
    Current date String = 5/17/91
    Code:
    date[charInDateIndex]
    The name of the index does not agree with the array it is indexing. The code should be using: date_index to index into the date array.

    The reason you use variable names that describe what they contain is so that the code can be read and understood.

    charInDateIndex would be used like this:
    Code:
    date[dateIndex].charAt(charInDateIndex);

IMN logo majestic logo threadwatch logo seochat tools logo