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 6 of 6

Thread: I have an error i dont knwo how to fix.

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question I have an error i dont knwo how to fix.

    So i have to make this contact book thing and i have just about got my head around the array list. Everything compiles, but when i enter everything in i get this error message:

    java.lang.StringIndexOutOfBoundsException:
    String index out of range:4 (in java.lang.String)

    import java.util.ArrayList;
    /**
     * Write a description of class ContactList here.
     * 
     * @author Tom Harvey
     * @version 1
     */
    public class ContactList
    {
        private ArrayList<Contact> contactList;
     
        public ContactList()
        {
            contactList = new ArrayList<Contact>();
        }
     
        public void addPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC,int phoneNumber)
        {
            contactList.add(new Contact(fName, sName, street, town, partOnePC, partTwoPC));
     
     
        }
     
    }


    /**
     * Write a description of class Contact here.
     * 
     * @author Tom Harvey
     * @version 1
     */
    public class Contact
    {
        protected String fName;
        protected String sName;
        protected String street;
        protected String town;
        protected String partOnePC;
        protected String partTwoPC;
        protected String postcode;
     
        public Contact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC)
        {
            this.fName = fName;
            this.sName = sName;
            this.street = street;
            this.town = town;
     
     
     
            this.partOnePC = partOnePC;
            [U]if(partOnePC != null && (partOnePC.charAt(2) > 0 || partOnePC.charAt(3) > 0) && partOnePC.charAt(4) > 0)[/U]
            {
                System.out.println("Please insert a string with 3-4 characters");
            }
     
            this.partOnePC = partOnePC;
            if(partTwoPC != null && (partTwoPC.charAt(2) > 0 || partTwoPC.charAt(3) > 0) && partTwoPC.charAt(4) > 0)
            {
                System.out.println("Please insert a string with 3-4 characters"); 
            }
     
            this.postcode = partOnePC + " " + partTwoPC;
        }
     
     
    }

    These are the two bits of code in the classes that are affected. I have underlined the code in which it has highlighted aswell


  2. #2
    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: I have an error i dont knwo how to fix.

    First thing I notice in your code, is the real life name of someone. Probably should delete things like that when you paste to a public forum.

    Second thing, underline does not work out inside code tags.

    Now to your error message:
    StringIndexOutOfBoundsException link
    What have you tried so far? Perhaps some more details on the error would help you figure it out? (ie catch the exception and look into it)
    or running in your debugger, (or even println) your variables and see what values are where when the exception is thrown.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: I have an error i dont knwo how to fix.

    Quote Originally Posted by 93tomh View Post
    So i have to make this contact book thing and i have just about got my head around the array list. Everything compiles, but when i enter everything in i get this error message:

    java.lang.StringIndexOutOfBoundsException:
    String index out of range:4 (in java.lang.String)
    ...
            this.partOnePC = partOnePC;
            [U]if(partOnePC != null && (partOnePC.charAt(2) > 0 || partOnePC.charAt(3) > 0) && partOnePC.charAt(4) > 0)[/U]
            {
                System.out.println("Please insert a string with 3-4 characters");
            }
     
            this.partOnePC = partOnePC;
            if(partTwoPC != null && (partTwoPC.charAt(2) > 0 || partTwoPC.charAt(3) > 0) && partTwoPC.charAt(4) > 0)
            {
                System.out.println("Please insert a string with 3-4 characters"); 
            }

    These are the two bits of code in the classes that are affected. I have underlined the code in which it has highlighted aswell
    as jbs said, the highlighted is useless.

    The exception: java.lang.StringIndexOutOfBoundsException: String index out of range:4 (in java.lang.String) tells you that your code is buggy Have you ever think about the "length" of this partOnePC or partTwoPC? You can't simply assume that the position 4 of your String is existent, can you? No? Then you know how to make sure of this case? No? How about a question partXYZPC.length() >= 4 before you start to quest the charAt(4)?

  4. The Following User Says Thank You to Voodoo For This Useful Post:

    93tomh (July 21st, 2012)

  5. #4
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: I have an error i dont knwo how to fix.

    OK, so when i take out the "&& partTwoPC.charAt(4) > 0" part, it works. Can you give me an example of how to ensure that the charAt(4) character is null as i really need this, but i am unsure of how to do it.

    -Thanks

  6. #5
    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: I have an error i dont knwo how to fix.

    Quote Originally Posted by 93tomh View Post
    ... Can you give me an example of how to ensure that the charAt(4) character is ...
    read the very last thing voodoo said in the above post

  7. The Following User Says Thank You to jps For This Useful Post:

    93tomh (July 21st, 2012)

  8. #6
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: I have an error i dont knwo how to fix.

    Oh yes, my bad, i overlooked that part of the message.

    Thanks to both of you!

Similar Threads

  1. My code has error when its run....I dont understand what's wrong of it.
    By jacky@~ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 11th, 2011, 08:48 AM
  2. Replies: 4
    Last Post: July 25th, 2011, 07:12 PM
  3. My code have a simple error but i dont know how to solve it !!!
    By Blackhawk1993 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 17th, 2011, 02:12 PM
  4. i'm getting this error, dont know why? please help
    By amr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 17th, 2010, 06:14 AM
  5. I dont get it....please help
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 30th, 2010, 04:16 AM