 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Terence Guest
|
Posted: Wed Mar 03, 2004 12:22 pm Post subject: 2D array |
|
|
help !! can somebody know what's wrong with this 2 D array ...
private static final String[] users = {
"Alice,20020115,100,20020314,-2,20020520,50,20030220,-34,20030724,-10,200308
01,5",
"Bob,20020213,98,20020422,-4,20020611,60,20030310,-43,20030807,-9,20030904,6
",
"Chris,20020317,10,20020521,30,20020601,5,20030320,-10,20030907,8,20031004,1
0"};
String[][] a = new String[2][5];
for (int i=0; i<5;i++)
{
int end_pos;
int start_pos=0;
for (int j=0;j<2;j++)
{
end_pos=users[i].indexOf(',',start_pos);
a[i][j]=users[j].substring(start_pos,end_pos);
start_pos=end_pos++;
a[i][j]="";
}
}
|
|
| Back to top |
|
 |
Ryan Stewart Guest
|
Posted: Wed Mar 03, 2004 12:41 pm Post subject: Re: 2D array |
|
|
"Terence" <terence_38i (AT) hotmail (DOT) com> wrote
| Quote: | help !! can somebody know what's wrong with this 2 D array ...
private static final String[] users = {
"Alice,20020115,100,20020314,-2,20020520,50,20030220,-34,20030724,-10,200308
01,5",
"Bob,20020213,98,20020422,-4,20020611,60,20030310,-43,20030807,-9,20030904,6
",
"Chris,20020317,10,20020521,30,20020601,5,20030320,-10,20030907,8,20031004,1
0"};
String[][] a = new String[2][5];
for (int i=0; i<5;i++)
{
int end_pos;
int start_pos=0;
for (int j=0;j<2;j++)
{
end_pos=users[i].indexOf(',',start_pos);
a[i][j]=users[j].substring(start_pos,end_pos);
start_pos=end_pos++;
a[i][j]="";
}
}
The first problem that springs to mind is that we need more information. A |
compilable example is preferable, and your question is vague at best.
Describe what's happening and what you want to happen and any errors you may
be getting. For tips see:
http://www.physci.org/codes/sscce.jsp
For a start in the problem area, on the second (code) line of your inner for
loop, you try to access a[i][j], which is a String[2][5]. But the range of i
is 0 to 4, so when i reaches 2, you'll receive an IndexOutOfBoundsException
on this line.
|
|
| Back to top |
|
 |
Gregory A. Swarthout Guest
|
Posted: Wed Mar 03, 2004 7:48 pm Post subject: Re: 2D array |
|
|
"Terence" <terence_38i (AT) hotmail (DOT) com> wrote
| Quote: | help !! can somebody know what's wrong with this 2 D array ...
private static final String[] users = {
"Alice,20020115,100,20020314,-2,20020520,50,20030220,-34,20030724,-10,200308
01,5",
"Bob,20020213,98,20020422,-4,20020611,60,20030310,-43,20030807,-9,20030904,6
",
"Chris,20020317,10,20020521,30,20020601,5,20030320,-10,20030907,8,20031004,1
0"};
String[][] a = new String[2][5];
for (int i=0; i<5;i++)
{
int end_pos;
int start_pos=0;
for (int j=0;j<2;j++)
{
end_pos=users[i].indexOf(',',start_pos);
a[i][j]=users[j].substring(start_pos,end_pos);
|
Here you are setting an element to a substring of users[j].
| Quote: | start_pos=end_pos++;
a[i][j]="";
|
Here you are setting the same element to an empty string.
What is the problem/error you are experiencing?
Greg
|
|
| Back to top |
|
 |
Tony Morris Guest
|
Posted: Wed May 12, 2004 7:58 am Post subject: Re: 2D array |
|
|
"D. Beckham" <lekhanh88 (AT) earthlink (DOT) net> wrote
| Quote: | Would someone shows me the java syntax to create a 2D array of a calendar,
where the rows represents months, such as: Jan, Feb... and columns
represent
days; assuming that there are 29 days in Feb? I don't know whether to
declare this 2D array in String or int, since the months are string while
days are int. Thanks
|
Java does not have 2D arrays.
It has arrays of arbitrary type, whose elements may be arrays i.e. arrays of
arrays.
This distinction is important, not academic/pedantic (I care not to describe
why).
At the end of the day, it will be you who does your homework, not some
arbitrary person on a newsgroup, since it is intended for you to learn.
http://java.sun.com/docs/books/tutorial/java/data/arrays.html
Good luck.
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Wed May 12, 2004 9:06 am Post subject: Re: 2D array |
|
|
On Wed, 12 May 2004 06:27:33 GMT, "D. Beckham"
<lekhanh88 (AT) earthlink (DOT) net> wrote or quoted :
| Quote: | Would someone shows me the java syntax to create a 2D array of a calendar,
where the rows represents months, such as: Jan, Feb... and columns represent
days; assuming that there are 29 days in Feb? I don't know whether to
declare this 2D array in String or int, since the months are string while
days are int. Thanks
I posted some code to create a calendar in HTML earlier today in |
comp.lang gui in the thread
"need help with 2D array".
you could take that and combine it with the code in
http://mindprod.com/jgloss/gotchas.html#MATRIX
to creata a matrix of objects of days, including dummies on front and
end so that grid always starts with Sunday.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
RC Guest
|
Posted: Wed May 12, 2004 2:07 pm Post subject: Re: 2D array |
|
|
D. Beckham wrote:
| Quote: | Would someone shows me the java syntax to create a 2D array of a calendar,
where the rows represents months, such as: Jan, Feb... and columns represent
days; assuming that there are 29 days in Feb? I don't know whether to
declare this 2D array in String or int, since the months are string while
days are int. Thanks
|
int calArray[][] = new int[12][31];
calArray[0] = Calendar.JANUARY; // it is int, not String
....
calArray[11] = Calendar.DECEMBER;
Or
calArray[0] = 0; // MONTH
....
calArray[11] = 11;
calArray[][0] = 1; // DAY_OF_MONTH
....
calArray[][30] = 31;
Note, Calendar.MONTH is from 0 to 11
and Calendar.DAY_OF_MONTH is from 1 to 31
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|