www.webdeveloper.com
Results 1 to 3 of 3

Thread: Trouble with substrings

  1. #1
    Join Date
    Feb 2012
    Posts
    7

    Trouble with substrings

    Basically I have no idea what I did wrong here. The assignment:

    Write a program to do the following:
    Prompt for input of someone's first, middle, and last name as a single string (using any combination of upper and lowercase letters).
    Check to make sure the name was entered in the correct format (3 names separated by spaces). If the input is not correct, continue to request the input again until the format is correct.
    Capitalize only the first letters of each part of the name, and print out the revised name.
    Print out the initials for that name.
    Print out the name in the format of: Lastname, Firstname, MI. (for example: Smith, John A.).

    Note: You must use the substring method, and you cannot utilize arrays or use StringTokenizer to accomplish this.



    Code:
        import java.util.*;
    public class Name 
    {
    	public static void main(String[] args) 
    	{
    		
    		Scanner input = new Scanner (System.in);
    	
    		int stringLength;
    		int blankSpace = 0;
    		boolean good = false;
    		
    			while (!good)
    			{
    				System.out.print("Enter your name as 'First Middle Last': ");
    				String aString = input.nextLine();
    				stringLength = aString.length();
    				
    				blankSpace = 0;
    				for (int i = 0; i < stringLength; i++)
    				{
    					aString.charAt(i);
    					char ch = (aString.charAt(i));
    					
    					if (ch == ' ')
    						{	
    							blankSpace++;
    						}	
    					}
    					
    					if (blankSpace == 2)
    						{
    							good = true;
    						}
    					
    					else
    						System.out.println("Invalid input");	
    			}
    		
    		int index = 0;
    		String firstName = "";
    		String middleName = "";
    		String lastName = "";
    		String aString = "";
    		char ch = ' ';
    		
    		while (aString.charAt(index)!= ' ')
    		{
    			firstName = firstName + aString.charAt(index);
    				++index;
    		}
    		
    		ch = Character.toUpperCase(firstName.charAt(0));
    		firstName = ch+ firstName.substring(1, firstName.length());
    		
    		aString = aString.substring(index + 1);
    		index = 0;
    		
    		while (aString.charAt(index)!= ' ')
    		{
    			middleName = middleName + aString.charAt(index + 1);
    				++index;
    		}
    	
    		ch =  Character.toUpperCase(middleName.charAt(0));
    		middleName = ch + middleName.substring(1, middleName.length());
    		
    		lastName = aString.substring(index + 1);
    		ch = Character.toUpperCase(lastName.charAt(0));
    		lastName = ch + lastName.substring(1, lastName.length());
    			
    		
    		System.out.println("\nYou entered: " + firstName + middleName + lastName);
    		
    		System.out.println("\nYour initials are: " + firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0));
    		
    		System.out.println("Displayed with last name first: " + lastName + ", " + firstName + middleName.charAt(0));
    			
    	}
    
    }
    The error message I get:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at Name.main(Name.java:47)

    Any help would be appreciated.

  2. #2
    Join Date
    Mar 2012
    Posts
    7
    Hey CaptainLaurie,
    your problem is that you say in line 44 that "aString = ""; " that means the string is empty and has no index 0 in line 47.

    I fixed only this mistake and it works, not with a perfect result but now you should be able to fix it :

    Code:
        import java.util.*;
    public class Name 
    {
    	public static void main(String[] args) 
    	{
    		
    		Scanner input = new Scanner (System.in);
    	
    		int stringLength;
    		int blankSpace = 0;
    		String aString = "";
    		boolean good = false;
    		
    			while (!good)
    			{
    				System.out.print("Enter your name as 'First Middle Last': ");
    				aString = input.nextLine();
    				stringLength = aString.length();
    				
    				blankSpace = 0;
    				for (int i = 0; i < stringLength; i++)
    				{
    					aString.charAt(i);
    					char ch = (aString.charAt(i));
    					
    					if (ch == ' ')
    						{	
    							blankSpace++;
    						}	
    					}
    					
    					if (blankSpace == 2)
    						{
    							good = true;
    						}
    					
    					else
    						System.out.println("Invalid input");	
    			}
    		
    		int index = 0;
    		String firstName = "";
    		String middleName = "";
    		String lastName = "";
    		char ch = ' ';
    		
    		while (aString.charAt(index)!= ' ')
    		{
    			firstName = firstName + aString.charAt(index);
    				++index;
    		}
    		
    		ch = Character.toUpperCase(firstName.charAt(0));
    		firstName = ch+ firstName.substring(1, firstName.length());
    		
    		aString = aString.substring(index+1);
    		index = 0;
    		
    		while (aString.charAt(index)!= ' ')
    		{
    			middleName = middleName + aString.charAt(index + 1);
    				++index;
    		}
    	
    		ch =  Character.toUpperCase(middleName.charAt(0));
    		middleName = ch + middleName.substring(1, middleName.length());
    		
    		lastName = aString.substring(index + 1);
    		ch = Character.toUpperCase(lastName.charAt(0));
    		lastName = ch + lastName.substring(1, lastName.length());
    			
    		
    		System.out.println("\nYou entered: " + firstName + middleName + lastName);
    		
    		System.out.println("\nYour initials are: " + firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0));
    		
    		System.out.println("Displayed with last name first: " + lastName + ", " + firstName + middleName.charAt(0));
    			
    	}
    
    }

    But i'am supprised, why you don't use an array with the string split method ?

  3. #3
    Join Date
    Feb 2012
    Posts
    7
    We weren't allowed to use arrays, we had to do it using the substring method.

    Thanks for the help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles