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

Thread: Intparse error. I have been trying to fix for hours.

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

    Default Intparse error. I have been trying to fix for hours.

    I am trying to create a method that when called will break an String consisting of an integer into individual digits, then converting these digits into integers and fill an array with these integers.

    I keep getting the error:
    java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at add.stringSplit(add.java:56)
    at add.main(add.java:27)"


    I feel really stupid and have been looking for solutions online, but I can't find anything relevant.
    Any help would seriously be appreciated. I wasted my whole Saturday on this.


        private int[] stringSplit (String fullString)
        {
            int[] tempArray = new int[fullString.length()];
            for(int i = 0; i < fullString.length(); i++)
            {
                String  x = fullString.substring(i,i);
                tempArray[(i)] = Integer.parseInt(x);
            }
            return tempArray;


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Intparse error. I have been trying to fix for hours.

    Read the documentation for the substring method. The end index is not included in the resultant substring.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Intparse error. I have been trying to fix for hours.

    // Java doc
    public String substring(int beginIndex,
                   int endIndex)
     
    Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
     
    Examples:
     
         "hamburger".substring(4, 8) returns "urge"
         "smiles".substring(1, 5) returns "mile"
     
     
    Parameters:
        beginIndex - the beginning index, inclusive.
        endIndex - the ending index, exclusive.
    Returns:
        the specified substring.
    Throws:
        IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

    You are telling it to return a substring of nothing.

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

    Default Re: Intparse error. I have been trying to fix for hours.

    Ah, thank you very much. I knew it would be something simple in the end, which made it even more frustrating!

    Say you are wanting to return a substring of the final letter, would you still tell it to stop at a point after the word that doesn't exist?

    For example, would this work?
    "hamburger".substring(8, 9) returns "r"

    Really appreciate the help guys.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Intparse error. I have been trying to fix for hours.

    yes, that's correct (try it yourself and see).

Similar Threads

  1. code issue, should be silple been working on it for four hours
    By Custermd in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 30th, 2011, 01:19 PM
  2. weekly hours for each employee
    By sircamolate in forum Collections and Generics
    Replies: 18
    Last Post: September 1st, 2011, 06:09 PM
  3. Hours worked help
    By glacier23 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 11th, 2010, 12:58 AM
  4. need in few hours help me
    By erinbasim in forum Java Theory & Questions
    Replies: 3
    Last Post: February 2nd, 2010, 06:39 PM
  5. project needed in two hours
    By erinbasim in forum Project Collaboration
    Replies: 1
    Last Post: February 2nd, 2010, 04:21 AM