Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default How to parse someone's full name by interpreting white spaces?

    I am writing a simple application that parses a first and last name by detecting a white space between them:
    Java Code:
    package murach.name;
    
    import java.util.Scanner;
    
    public class NameParserApp {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String choice = "y";
            while(!choice.equalsIgnoreCase("n")){
            	
    	        System.out.print("Enter a name: ");
    	        int whiteSpaceFound = 0;
    	        String name = sc.nextLine();
    	        for(int i = 0; i < name.length(); i++){
    				if(name.charAt(i) == ' '){
    					whiteSpaceFound++;
    				}
    			}
    	        if(whiteSpaceFound == 1){//if only one white space and two words are found
    	        	//Find the index of the white space in the name string:
    	    		int whiteSpace1 = name.indexOf(" ");
    	    		int whiteAfterLastName = name.indexOf(" ");
    	        	String firstName = name.substring(0, whiteSpace1);
    	        	String lastName = name.substring(whiteSpace1, whiteAfterLastName);
    	        	System.out.println("First name: " + firstName);
    	        	System.out.println("Last Name: " + lastName);
    	        }
    	        else{
    	        	System.out.println("Error. Name must be either two words with a space between them,");
    	        	System.out.println("or three words with a space between them.");
    	        }
    	    	//see if user wants to continue:
    			System.out.println("Continue? (y/n): ");
    			choice = sc.nextLine();
    			System.out.println();
            }//end of while loop
            System.out.println();
            sc.close();
        }
    }
    However, something unexpected happened in the following sample output:
    How to parse someone's full name by interpreting white spaces?-javaoutputproblem1.jpg
    Why is the lastName blank?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: How to parse someone's full name by interpreting white spaces?

    These two lines contain the same number. This doesn't look right.
    Java Code:
    int whiteSpace1 = name.indexOf(" ");
    int whiteAfterLastName = name.indexOf(" ");
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: How to parse someone's full name by interpreting white spaces?

    Quote Originally Posted by SurfMan View Post
    These two lines contain the same number. This doesn't look right.
    Java Code:
    int whiteSpace1 = name.indexOf(" ");
    int whiteAfterLastName = name.indexOf(" ");
    What's the fastest way to access the index value of the last char in a string? The reason I say "index value" instead of "the last char" or something is because both arguments of the substring method call must be the same data type (in this case, I need them to both be integers):
    Java Code:
    String lastName = name.substring(whiteSpace2, index of final char in string?);

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: How to parse someone's full name by interpreting white spaces?

    String.lastIndexOf()?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: How to parse someone's full name by interpreting white spaces?

    While it's certainly possible to achieve what you're doing using the indexes, I might turn out much tidier to use the .split() feature of String (String (Java Platform SE 8 )).

  6. #6
    Pavlo is offline Member
    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0

    Default Re: How to parse someone's full name by interpreting white spaces?

    One more for String split()

    You will get a String array with names part, and after that can take array length param or any element by index

    String [] nameParts = name.split(" ");

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: How to parse someone's full name by interpreting white spaces?

    If this is a school assignment, please ignore this post. If this is for some project which is going into production some day, keep reading.

    The assumption of people having a firstname and lastname is wrong. Remember that parsing names like this is extremely difficult. In that light, this article is must read.

    Think about what to do when my name is:
    Pete (Just Pete, no surname)
    Pete van Dam
    Pete van der Klein Lugtebelt
    Meet his wife: Marie Josephine van der Klein Lugtebelt

    It's not as trivial as it looks.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to parse someone's full name by interpreting white spaces?

    Its especially fun when you have colleagues overseas in say Singapore. It can take years before people realise that what they thought was someone's first name is actually their last name.

    But that's all besides the point, this question is only about the difficulties of using a scanner and splitting a String up into chunks :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: How to parse someone's full name by interpreting white spaces?

    Quote Originally Posted by gimbal2 View Post
    Its especially fun when you have colleagues overseas in say Singapore. It can take years before people realise that what they thought was someone's first name is actually their last name.

    But that's all besides the point, this question is only about the difficulties of using a scanner and splitting a String up into chunks :)
    Things escalate quickly around here :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Interpreting human speech
    By stuffses in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2012, 05:49 AM
  2. Replies: 1
    Last Post: 03-29-2012, 09:11 PM
  3. Replies: 2
    Last Post: 06-29-2011, 09:08 AM
  4. How to display a String that contains more than one white spaces
    By thachun in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-27-2009, 09:16 AM
  5. Jar file is not interpreting
    By JavaJunkie in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 06:39 PM

Posting Permissions

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