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

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-22-2011, 05:48 AM
okokok
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 .
• 09-22-2011, 02:38 PM
Norm
Re: I Dont know how to put my data into 2D arrays
Quote:

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.
• 09-22-2011, 05:48 PM
okokok
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:
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
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.

• 09-22-2011, 05:55 PM
Norm
Re: I Dont know how to put my data into 2D arrays
Quote:

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.
• 09-22-2011, 07:18 PM
okokok
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.
• 09-22-2011, 07:27 PM
okokok
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:
Attachment 1804
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
• 09-22-2011, 07:37 PM
Norm
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.
• 09-22-2011, 07:40 PM
okokok
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)
• 09-22-2011, 07:42 PM
okokok
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();

}
}

}
• 09-22-2011, 07:45 PM
okokok
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
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)

• 09-22-2011, 07:48 PM
Norm
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:
Quote:

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?
• 09-22-2011, 07:55 PM
okokok
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();
}
}

}

}
• 09-22-2011, 07:59 PM
Norm
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.
• 09-22-2011, 08:02 PM
Norm
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);
• 09-22-2011, 08:07 PM
okokok
Re: I Dont know how to put my data into 2D arrays
Here is my draft. Attachment 1806
Quote:

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.

• 09-22-2011, 08:11 PM
Norm
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
• 09-22-2011, 08:25 PM
okokok
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
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

• 09-22-2011, 08:29 PM
Norm
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?
• 09-22-2011, 08:44 PM
okokok
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
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?

• 09-22-2011, 08:46 PM
Norm
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.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last