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

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

    I am a new guy in Java, But I will study hard .
    here is my code:

    import java.lang.*;


    import java.lang.*;


    public class TwoD {

    static int i;
    static int j;
    static String[][] arrayB;


    /**
    * @param args
    */
    public static void main(String[] args) {
    String a = new String("1111100");

    String b1 = a.substring(0,4);
    // TODO Auto-generated method stub
    String b2 = a.substring(4, 7);

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

    for(int i=0; i<=2;i++)
    {
    for(int j=0;j<=3;j++)
    {

    String[][] arrayB= new String[i][j];


    }
    }
    for(int begin =0;begin<=3;begin++)
    {
    int end=1;

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



    }

    }
    I need put each string of b1 put into the arrayB[][], But it is hard to implement.
    who can help me to resolve it .

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

    I need put each string of b1 put into the arrayB[][], But it is hard to implement.
    Can you describe where each character in the String is to go?
    Take a piece of paper and write down all the characters in the String
    then underneath each character write down the x,y position in the 2 dim array that character is to go to.
    Can you do that? Then see if there is a pattern with the position of the character in the String with its x,y target location.

  3. #3
    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, I have already done, and i just revise my code. but system still tell me I have exception"java.lang.ArrayIndexOutOfBoundsException : 0",I am confused.
    here is my revised code:
    Java Code:
    public static void main(String[] args) {
    		String a = new String("1111100");
    		
    		String b1 = a.substring(0,4);
    
    		String b2 = a.substring(4, 7);
    		
    		System.out.print(b1);
    		System.out.print(b2);
    		String[][] arrayB;
    		for(int i=0; i<=2;i++)
    		{
    			for(int j=0;j<=3;j++)
    			{
    			int end = 1;
    			int begin =0;
    			arrayB= new String[i][j];
    			arrayB[i][j]=b1.substring(begin,end);
    			end++;
    			begin++;
    				System.out.print(begin);
    				System.out.print(end);
    				
    				
    			}
    		}
    }
    Quote Originally Posted by Norm View Post
    Can you describe where each character in the String is to go?
    Take a piece of paper and write down all the characters in the String
    then underneath each character write down the x,y position in the 2 dim array that character is to go to.
    Can you do that? Then see if there is a pattern with the position of the character in the String with its x,y target location.
    Last edited by Norm; 09-22-2011 at 06:55 PM. Reason: added code tags

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

    java.lang.ArrayIndexOutOfBoundsException : 0
    What line did that occur on?
    The message says that the array has 0 elements and that you are trying to index the first element at index=0.

  5. #5
    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

    Ok, thanks, I find problem out.
    But did you know how to set back its initial variable when filed Variables is in a loop which is finished.

  6. #6
    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

    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.print(b1);
    System.out.println();
    System.out.print(b2);
    System.out.println();
    String[][] arrayB= new String[3][4];;
    for(int i=0; i<=2;i++)
    {
    for(int j=0;j<=3;j++)
    {


    while(begin<=i&&j<=i+1)
    {
    arrayB[i][j]=b1.substring(begin,end);
    begin++;
    end++;



    System.out.printf("i:%d",i);
    System.out.println();
    System.out.printf("j:%d",j);
    System.out.println();
    System.out.printf("begin:%d",begin);
    System.out.println();
    System.out.printf("end:%d",end);
    System.out.println();

    }
    }

    }

    }
    }

    the output:
    I Dont know how to put my data into 2D arrays-.jpg
    but I need output like this

    i:0
    j:0
    begin:1
    end:2
    i:0
    j:1
    begin:2
    end:3
    i:0
    j:2
    begin:3
    end:4
    i:0
    j:3
    begin:4
    end:5
    i:1
    j:0
    begin:1
    end:2
    i:1
    j:1
    begin:2
    end:3
    i:1
    j:2
    begin:3
    end:4
    i:1
    j:3
    begin:4
    end:5
    i:2
    j:0
    begin:1
    end:2
    i:2
    j:1
    begin:2
    end:3
    i:2
    j:2
    begin:3
    end:4
    i:2
    j:3
    begin:4
    end:5
    i:3
    j:0
    begin:1
    end:2
    i:3
    j:1
    begin:2
    end:3
    i:3
    j:2
    begin:3
    end:4
    i:3
    j:3
    begin:4
    end:5

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

    The image shows an error. Why is the index value going past the end of the array?


    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  8. #8
    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

    1111
    100
    i:0
    j:0
    begin:1
    end:2
    i:0
    j:1
    begin:2
    end:3
    i:0
    j:2
    begin:3
    end:4
    i:0
    j:3
    begin:4
    end:5
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.substring(Unknown Source)
    at TwoD.main(TwoD.java:28)

  9. #9
    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 here is my code.
    public class increase {

    public void increaseT(int begin,int end,int i,int j,String[][]arrayB)
    {
    while(begin<=i&&j<=i+1)
    {
    arrayB[i][j]=b1.substring(begin,end);
    begin++;
    end++;



    System.out.printf("i:%d",i);
    System.out.println();
    System.out.printf("j:%d",j);
    System.out.println();
    System.out.printf("begin:%d",begin);
    System.out.println();
    System.out.printf("end:%d",end);
    System.out.println();

    }
    }

    }

  10. #10
    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 why this exception, because the begin and end are increasing,no stop.
    So I think one of way solving this problem is create new class which do the increase and give array values. Is that a good way?
    Quote Originally Posted by okokok View Post
    1111
    100
    i:0
    j:0
    begin:1
    end:2
    i:0
    j:1
    begin:2
    end:3
    i:0
    j:2
    begin:3
    end:4
    i:0
    j:3
    begin:4
    end:5
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.substring(Unknown Source)
    at TwoD.main(TwoD.java:28)

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

    That's strange. When I execute the code, I don't get the error:
    b1=1111
    b2=100
    i:0
    j:0
    begin:1
    end:2
    i:1
    j:0
    begin:2
    end:3
    i:2
    j:0
    begin:3
    end:4

    0 error(s)
    You need to go back to the piece of paper and work out your logic.
    Start with a String with unique characters:
    String a = new String("1234567");
    Draw a grid for the 2 dim array and copy into each slot one of the numbers
    Then write down the row, col for each number
    Then see the logic for assigning each char to a row, column
    For example:
    1-> 0,0
    2-> 0,1
    3-> 0,2
    4-> 1,0
    5-> 1,1
    6-> 1,2



    Something does NOT make sense. The array has 12 slots but the String only has 7 characters.
    Do you need a longer number or do you need a smaller array?

  12. #12
    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 paste a wrong code;
    I think that's problem caused by this code.

    import java.lang.*;


    public class TwoD {




    /**
    * @param args
    */
    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.print(b1);
    System.out.println();
    System.out.print(b2);
    System.out.println();
    String[][] arrayB= new String[3][4];;
    for(int i=0; i<=2;i++)
    {
    for(int j=0;j<=3;j++)
    {

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


    System.out.printf("i:%d",i);
    System.out.println();
    System.out.printf("j:%d",j);
    System.out.println();
    System.out.printf("begin:%d",begin);
    System.out.println();
    System.out.printf("end:%d",end);
    System.out.println();
    }
    }

    }

    }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

    Does it work now? If not you need to rework the code.

    Start with a String with of unique characters:
    String a = new String("1234567");
    Draw a grid for the 2 dim array and copy into each slot one of the numbers
    Then write down the row, col for each number
    Then see the logic for assigning each char to a row, column
    For example: list the number on the left and the row,column on the right
    1-> 0,0
    2-> 0,1
    3-> 0,2
    4-> 1,0
    5-> 1,1
    6-> 1,2
    ...

    A problem is that the array has 12 elements and the String has 7.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

    Comments on your code:
    This can be done in one line vs two:
    System.out.print(b1);
    System.out.println();
    vs
    System.out.println("b1=" +b1);

    And the same for this:
    System.out.printf("i:%d",i);
    System.out.println();
    vs
    System.out.printf("i:%d\n",i);

  15. #15
    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

    Here is my draft. I Dont know how to put my data into 2D arrays-dsc05359_-.jpg
    Quote Originally Posted by Norm View Post
    Does it work now? If not you need to rework the code.

    Start with a String with of unique characters:
    String a = new String("1234567");
    Draw a grid for the 2 dim array and copy into each slot one of the numbers
    Then write down the row, col for each number
    Then see the logic for assigning each char to a row, column
    For example: list the number on the left and the row,column on the right
    1-> 0,0
    2-> 0,1
    3-> 0,2
    4-> 1,0
    5-> 1,1
    6-> 1,2
    ...

    A problem is that the array has 12 elements and the String has 7.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

    Start with a String with of unique characters:
    String a = new String("1234567");
    Then you can see exactly where each element the array came from. When there are only 0 and 1 you can not tell.
    Then if you list them like this:
    1-> 0,0
    2-> 0,1
    3-> 0,2
    4-> 1,0
    5-> 1,1
    6-> 1,2

  17. #17
    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

    HERE is my revised one, the logic is right.Just like my draft I wrote.
    But the problem is the 'begin' and 'end' can't jump out, and reset to 'begin=0' and 'end = 1'.
    Sorry, My english is not good, maybe somewhere let you confused.
    here are my 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++)
    {

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


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

    }
    }

    }

    }

    Here is the error:

    1111
    100
    i=0
    j=0
    begin=1
    end=2
    i=0
    j=1
    begin=2
    end=3
    i=0
    j=2
    begin=3
    end=4
    i=0
    j=3
    begin=4
    end=5
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.substring(Unknown Source)
    at TwoD.main(TwoD.java:33)

    Quote Originally Posted by Norm View Post
    Start with a String with of unique characters:
    String a = new String("1234567");
    Then you can see exactly where each element the array came from. When there are only 0 and 1 you can not tell.
    Then if you list them like this:
    1-> 0,0
    2-> 0,1
    3-> 0,2
    4-> 1,0
    5-> 1,1
    6-> 1,2

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

    I think you need to completely rewrite your code and not try to fix it.

    Can you explain in what order you are going to take the characters of the String and where you are going to put them into the array?
    Can you give the target row and column for each character in the String?
    The array has more slots than there are characters in the String. What will be done with those extra places in the array?

  19. #19
    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

    first, I need break a String into two part. one part is context i need transfer, and another part code is for which i use to encoding if it is original one.
    and I have a method to check if it is original one. I define 3 rows and 4 columns to put the context in, then I use 2d array and another part code to encode the context if it is right. it IS HAMMING'S ALGORITHM.
    Quote Originally Posted by Norm View Post
    I think you need to completely rewrite your code and not try to fix it.

    Can you explain in what order you are going to take the characters of the String and where you are going to put them into the array?
    Can you give the target row and column for each character in the String?
    The array has more slots than there are characters in the String. What will be done with those extra places in the array?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

    What parts of the String go in the array?
    Where do those parts go? The String has 7 char and the array has 12.
    The substring b1 has even fewer characters.

Page 1 of 2 12 LastLast

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
  •