Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32
  1. #21
    okokok is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: I Dont know how to put my data into 2D arrays

    the first 4 characters into array.
    and every row has same ordered number.
    like:
    String a = "7890100"
    7->0,0
    8->0,1
    9->0,2
    0->0,3

    7->1,0
    8->1,1
    9->1,2
    0->1,3

    7->2,0
    8->2,1
    9->2,2
    0->2,3

    7->3,0
    8->3,1
    9->3,2
    0->3,3

  2. #22
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: I Dont know how to put my data into 2D arrays

    So the index for the characters of the source String would go from 0 to 3.

  3. #23
    okokok is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: I Dont know how to put my data into 2D arrays

    Yes, a.substring(0,3)
    Quote Originally Posted by Norm View Post
    So the index for the characters of the source String would go from 0 to 3.

  4. #24
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: I Dont know how to put my data into 2D arrays

    I was referring to this index:
    java.lang.StringIndexOutOfBoundsException: String index out of range: 5

  5. #25
    okokok is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: I Dont know how to put my data into 2D arrays

    what's mean?
    Quote Originally Posted by Norm View Post
    So the index for the characters of the source String would go from 0 to 3.

  6. #26
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: I Dont know how to put my data into 2D arrays

    Where is this error happening?
    java.lang.StringIndexOutOfBoundsException: String index out of range: 5

    The index is past the end of the String. Your code must keep the index within the length of the String.

  7. #27
    okokok is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: I Dont know how to put my data into 2D arrays

    I know that, the b1 string index out of range. because the 'begin'and'end' dont stop adding 1
    until the all loop is done.
    Quote Originally Posted by Norm View Post
    I was referring to this index:
    java.lang.StringIndexOutOfBoundsException: String index out of range: 5

  8. #28
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: I Dont know how to put my data into 2D arrays

    Your code must keep the value of begin in bounds.

  9. #29
    okokok is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: I Dont know how to put my data into 2D arrays

    when the begin = 4,and 5 , the b1 is out of range. thanks!!! But i still confused how to reset 'begin''end' to initial value
    Quote Originally Posted by okokok View Post
    I know that, the b1 string index out of range. because the 'begin'and'end' dont stop adding 1
    until the all loop is done.

  10. #30
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: I Dont know how to put my data into 2D arrays

    Use an if statement

  11. #31
    okokok is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: I Dont know how to put my data into 2D arrays

    I revise the code.

    import java.lang.*;


    public class TwoD {

    public static void main(String[] args) {
    int begin=0;
    int end=1;
    String a = new String("1111100");

    String b1 = a.substring(0,4);

    String b2 = a.substring(4, 7);

    System.out.println(b1);
    System.out.println(b2);

    String[][] arrayB= new String[3][4];;
    for(int i=0; i<=2;i++)
    {
    for(int j=0;j<=3;j++)
    {
    System.out.println("i="+i);
    System.out.println("j="+j);
    System.out.println("beginInput="+begin);
    System.out.println("endInput="+end);

    arrayB[i][j]=b1.substring(begin,end);
    begin++;
    end++;


    System.out.println("begin="+begin);
    System.out.println("end="+end);

    if (begin>3&&end>4)
    {
    begin =0;
    end = 1;
    }

    }
    }

    }

    }

    The output:1111
    100
    i=0
    j=0
    beginInput=0
    endInput=1
    begin=1
    end=2
    i=0
    j=1
    beginInput=1
    endInput=2
    begin=2
    end=3
    i=0
    j=2
    beginInput=2
    endInput=3
    begin=3
    end=4
    i=0
    j=3
    beginInput=3
    endInput=4
    begin=4
    end=5
    i=1
    j=0
    beginInput=0
    endInput=1
    begin=1
    end=2
    i=1
    j=1
    beginInput=1
    endInput=2
    begin=2
    end=3
    i=1
    j=2
    beginInput=2
    endInput=3
    begin=3
    end=4
    i=1
    j=3
    beginInput=3
    endInput=4
    begin=4
    end=5
    i=2
    j=0
    beginInput=0
    endInput=1
    begin=1
    end=2
    i=2
    j=1
    beginInput=1
    endInput=2
    begin=2
    end=3
    i=2
    j=2
    beginInput=2
    endInput=3
    begin=3
    end=4
    i=2
    j=3
    beginInput=3
    endInput=4
    begin=4
    end=5



    cool!!!! I have a conclude that you must know the nature of error, then you can fix the error completely.
    Thanks, Norm!
    Quote Originally Posted by Norm View Post
    Your code must keep the value of begin in bounds.

  12. #32
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: I Dont know how to put my data into 2D arrays

    You could reduce the code a bit by getting rid of the end variable since it is always = begin+1
    Java Code:
    arrayB[i][j]=b1.substring(begin, begin+1);
    I changed your debug output to:
    Java Code:
    		System.out.println("i="+ i + ", j="+j + " char=" + arrayB[i][j]);
    		System.out.println("begin= "+begin);

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 0
    Last Post: 05-11-2011, 12:49 PM
  2. Storing/retrieving arrays in a data structure...
    By pbandjay in forum New To Java
    Replies: 0
    Last Post: 11-03-2010, 01:39 AM
  3. Use of Arrays to store data?
    By moondy in forum New To Java
    Replies: 4
    Last Post: 10-07-2010, 12:11 PM
  4. Why is my program still writing data i dont want
    By SwissR in forum New To Java
    Replies: 32
    Last Post: 07-29-2010, 01:05 PM
  5. Data Files - A problem that I dont understand :D
    By Exhonour in forum New To Java
    Replies: 7
    Last Post: 01-20-2009, 06:13 AM

Posting Permissions

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