Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how do i split a string into two parts and test if the second part of string is empty

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how do i split a string into two parts and test if the second part of string is empty

    Hi guys i am trying to divide a single string into two parts but am having abit of a problem here. i am getting a wrong error message. i wanted to get the invalidnameexception error but instead i am getting a arrayindexoutofbound error.

    Is there other way that i can split the string and test if the second string is empty would like to print out the invalidnameexception. i have done alot of research but i just cant seem to find the problem.
    import java.util.Scanner;
    import javax.naming.InvalidNameException;
    public class Name{
     
    	public static void printName(String name) throws InvalidNameException{
     
    		String[] seperator = name.split(" ");
    		String firstName = seperator[0];
    		String lastName = seperator[1];
     
     
    		for (int i=0; i<seperator.length; i++) {
            if (seperator[i] == null) 
               throw new InvalidNameException("invalid name!");
     
     
     
     
            }
     
    		System.out.println(firstName);
    		System.out.println(lastName);
     
     
     
     
     
     
    		}
     
     
    	public static void main(String[] args){
    		    	Scanner kb = new Scanner(System.in);
    		    	System.out.println("please enter name:");
    		    	String fullname = kb.nextLine();
     
     
    		try{
    			Name.printName(fullname);
     
    			}catch (InvalidNameException e1){
    				System.out.println(e1);
    				}
     
    		}
     
     
     
     
     
     
    	}
    Last edited by helloworld922; October 26th, 2012 at 10:32 AM. Reason: please use [code] tags


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do i split a string into two parts and test if the second part of string is e

    The reason you're getting an ArrayOutOfBoundException is because after using the split on a single word (without spaces) you do not have 2 values in your array, so you should first make sure there are atleast 2 values in your array, if there isn't then you already know the person entered an invalid name.

  3. #3
    Junior Member psabbate's Avatar
    Join Date
    Aug 2012
    Posts
    20
    My Mood
    Amused
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: how do i split a string into two parts and test if the second part of string is e

    You should use something like this
    /**Code removed by moderator*/

    Sorry about the format!
    Last edited by jps; October 26th, 2012 at 02:18 PM. Reason: spoonfeeding
    Everyone wants to go to heaven ... but nobody wants to die

    Nissi Group, Servicios IT,
    Diseņo Web, Desarrollo de Software, SEO,
    LinkedIn

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how do i split a string into two parts and test if the second part of string is e

    @psabbate Please read the problem with spoonfeeding.

Similar Threads

  1. String.split("") puts null in the first index
    By sonicjr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 10th, 2023, 03:11 AM
  2. Replacing parts of a String using line.replaceAll
    By brandonSMC in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2012, 04:07 PM
  3. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  4. Draw and arc under part of a string
    By lammybun in forum Java Theory & Questions
    Replies: 3
    Last Post: February 26th, 2012, 12:26 PM
  5. How to find a a certain part in string
    By redzero36 in forum Java Theory & Questions
    Replies: 7
    Last Post: July 19th, 2011, 06:39 PM