Results 21 to 32 of 32
- 09-22-2011, 08:52 PM #21
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
- 09-22-2011, 08:55 PM #22
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.
- 09-22-2011, 08:59 PM #23
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
- 09-22-2011, 09:00 PM #24
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
- 09-22-2011, 09:08 PM #25
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
- 09-22-2011, 09:10 PM #26
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.
- 09-22-2011, 09:11 PM #27
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
- 09-22-2011, 09:12 PM #28
Re: I Dont know how to put my data into 2D arrays
Your code must keep the value of begin in bounds.
- 09-22-2011, 09:14 PM #29
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
- 09-22-2011, 09:17 PM #30
Re: I Dont know how to put my data into 2D arrays
Use an if statement
- 09-22-2011, 09:25 PM #31
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
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!
- 09-22-2011, 09:29 PM #32
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
I changed your debug output to:Java Code:arrayB[i][j]=b1.substring(begin, begin+1);
Java Code:System.out.println("i="+ i + ", j="+j + " char=" + arrayB[i][j]); System.out.println("begin= "+begin);
Similar Threads
-
How can i store a data from Excel sheet to an array. Please Dont mark as Repeated
By deshmukh.niraj04 in forum New To JavaReplies: 0Last Post: 05-11-2011, 11:49 AM -
Storing/retrieving arrays in a data structure...
By pbandjay in forum New To JavaReplies: 0Last Post: 11-03-2010, 12:39 AM -
Use of Arrays to store data?
By moondy in forum New To JavaReplies: 4Last Post: 10-07-2010, 11:11 AM -
Why is my program still writing data i dont want
By SwissR in forum New To JavaReplies: 32Last Post: 07-29-2010, 12:05 PM -
Data Files - A problem that I dont understand :D
By Exhonour in forum New To JavaReplies: 7Last Post: 01-20-2009, 05:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks