Results 1 to 5 of 5

Thread: substring issue

  1. #1
    Clown is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default substring issue

    Hi everyone, I am trying to divide an integer number into several Strings, each of them containing 3 characters. (For example 12345678 should be "123", "456","78")I wrote the following simple code, which seems pretty fine to me. The problem is that I get an Exception at my first substring function. I tried debugger and it seems that the exceptions is caused by the function b.substring(0,2), whereas my integer is something like 1234234. To me b.substring (0,2) should return ("123")

    Can someone please tell me what I am doing wrong? I am new to Java so this is probably gonna be really simple, but I dont see it.

    Thanks in advance.

    Peter

    public class Numbers {

    private String b;
    private String c[];

    public void divide(int a){
    b=String.valueOf(a);
    int j=b.length();
    int k=j/3;
    int l=0;
    for (int i=0;i<k;i++){
    c[i]=b.substring(l, l+2);
    l=l+2;
    }
    c[k]=b.substring(l, j-1);

    }
    }

  2. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    c is never declared. You should have gotten a null-pointer exception.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by imorio View Post
    c is never declared.
    It is declared,
    Java Code:
    private String c[];

    But not initialized. Like,

    Java Code:
    private String c[] = new String[5];

    Initializing the array with some size of your choice should resolve this issue.

    Post the errors if you get any.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by Clown View Post
    whereas my integer is something like 1234234. To me b.substring (0,2) should return ("123")
    No!

    It will return "12" . Read the documentation for String.substring(int int) carefully. If you want 3 characters, pass 3 as second argument.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    Clown is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Substring indexing
    By DreamNaut in forum New To Java
    Replies: 10
    Last Post: 01-10-2011, 11:39 AM
  2. substring Q
    By arsenal4ever_11 in forum NetBeans
    Replies: 1
    Last Post: 06-01-2010, 03:27 PM
  3. Help with substring
    By hiklior in forum New To Java
    Replies: 12
    Last Post: 06-28-2008, 06:48 AM
  4. How can i get substring
    By 82rathi.angara in forum New To Java
    Replies: 14
    Last Post: 06-27-2008, 04:35 AM
  5. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:15 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
  •