# Thread: I Dont know how to put my data into 2D arrays

1. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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 .  Reply With Quote

2. ## 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.  Reply With Quote

3. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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);

}
}
}``` Originally Posted by Norm 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 05:55 PM. Reason: added code tags  Reply With Quote

4. ## 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.  Reply With Quote

5. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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.  Reply With Quote

6. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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;;
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: 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  Reply With Quote

7. ## 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.  Reply With Quote

8. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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)  Reply With Quote

9. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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();

}
}

}  Reply With Quote

10. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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? Originally Posted by okokok 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)  Reply With Quote

11. ## 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.
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?  Reply With Quote

12. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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;;
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();
}
}

}

}  Reply With Quote

13. ## Re: I Dont know how to put my data into 2D arrays

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

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.  Reply With Quote

14. ## Re: I Dont know how to put my data into 2D arrays

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);  Reply With Quote

15. Member Join Date
Sep 2011
Posts
21
Rep Power
0

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

Here is my draft.  Originally Posted by Norm Does it work now? If not you need to rework the code.

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.  Reply With Quote

16. ## Re: I Dont know how to put my data into 2D arrays

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  Reply With Quote

17. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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;;
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) Originally Posted by Norm 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  Reply With Quote

18. ## 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?  Reply With Quote

19. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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. Originally Posted by Norm 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?  Reply With Quote

20. ## 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.  Reply With Quote

#### Posting Permissions

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