Two dimensional calendar array

J

Juggernaut

I have a two dimensional array, [12][31], and I have it made as a
calender where the days are filled with some information. But some
months, like february only has 28 days so I havent filled 29 30 and
31. But now I want to make some calculations on these numbers, and
divide them by all the days. But the problem is they also count the
array fields like february 29 , 30 and 31 wich is empty..I tried a for
loop where if col < table[row].length && table[row][col] != null. But
that didnt work, how can I do this?

Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

Any advice would be gratefull.
 
O

Oscar kind

Juggernaut said:
I have a two dimensional array, [12][31], and I have it made as a
calender where the days are filled with some information. But some
months, like february only has 28 days so I havent filled 29 30 and
31. But now I want to make some calculations on these numbers, and
divide them by all the days. But the problem is they also count the
array fields like february 29 , 30 and 31 wich is empty..I tried a for
loop where if col < table[row].length && table[row][col] != null. But
that didnt work, how can I do this?

Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

I'd say it is the right direction: as Java doesn't support two-dimensional
arrays, you really have an array of arrays. The correct syntax however,
is:
yourArray[1] = new SomeType[28];


Oscar
 
A

Andrew Harker

Juggernaut said:
I have a two dimensional array, [12][31], and I have it made as a
calender where the days are filled with some information. But some
months, like february only has 28 days so I havent filled 29 30 and
31. But now I want to make some calculations on these numbers, and
divide them by all the days. But the problem is they also count the
array fields like february 29 , 30 and 31 wich is empty..I tried a for
loop where if col < table[row].length && table[row][col] != null. But
that didnt work, how can I do this?

Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

Any advice would be gratefull.

Yep, use a ragged array ...

// note all zero based
int [] monthLengthNonLeapYear =
{31,28,31,30,31,30,31,31,30,31,30,31};

// Your day object .. array of arrays
Object [][] myCalendar = new Object[monthLengthNonLeapYear.length][];

// Create an array the right length for each month
for (int i = 0; i < monthLengthNonLeapYear.length; i++) {
myCalendar = new Object[monthLengthNonLeapYear];
}

// Play with it
for (int i = 0; i < myCalendar.length; i++) {
System.out.print("Month:"+i+" Days:");
for (int j = 0; j < myCalendar.length; j++) {
System.out.print(j+" ");
}
System.out.println();
}
 
J

Juggernaut

Thanks alot :)
I will try this
Andrew Harker said:
Juggernaut said:
I have a two dimensional array, [12][31], and I have it made as a
calender where the days are filled with some information. But some
months, like february only has 28 days so I havent filled 29 30 and
31. But now I want to make some calculations on these numbers, and
divide them by all the days. But the problem is they also count the
array fields like february 29 , 30 and 31 wich is empty..I tried a for
loop where if col < table[row].length && table[row][col] != null. But
that didnt work, how can I do this?

Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

Any advice would be gratefull.

Yep, use a ragged array ...

// note all zero based
int [] monthLengthNonLeapYear =
{31,28,31,30,31,30,31,31,30,31,30,31};

// Your day object .. array of arrays
Object [][] myCalendar = new Object[monthLengthNonLeapYear.length][];

// Create an array the right length for each month
for (int i = 0; i < monthLengthNonLeapYear.length; i++) {
myCalendar = new Object[monthLengthNonLeapYear];
}

// Play with it
for (int i = 0; i < myCalendar.length; i++) {
System.out.print("Month:"+i+" Days:");
for (int j = 0; j < myCalendar.length; j++) {
System.out.print(j+" ");
}
System.out.println();
}
 
J

Juggernaut

Thanks
=)

Oscar kind said:
Juggernaut said:
I have a two dimensional array, [12][31], and I have it made as a
calender where the days are filled with some information. But some
months, like february only has 28 days so I havent filled 29 30 and
31. But now I want to make some calculations on these numbers, and
divide them by all the days. But the problem is they also count the
array fields like february 29 , 30 and 31 wich is empty..I tried a for
loop where if col < table[row].length && table[row][col] != null. But
that didnt work, how can I do this?

Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

I'd say it is the right direction: as Java doesn't support two-dimensional
arrays, you really have an array of arrays. The correct syntax however,
is:
yourArray[1] = new SomeType[28];


Oscar
 
J

Juggernaut

But do these arrays actually be filled with any info? Isnt it just the loop
posting numbers?

You see I want to fill these day arrays with values, random values (since I
later have to do some calculations with them).

So I want the array, with 365 "cells" of random values, to be calculated
with later.


Andrew Harker said:
Juggernaut said:
I have a two dimensional array, [12][31], and I have it made as a
calender where the days are filled with some information. But some
months, like february only has 28 days so I havent filled 29 30 and
31. But now I want to make some calculations on these numbers, and
divide them by all the days. But the problem is they also count the
array fields like february 29 , 30 and 31 wich is empty..I tried a for
loop where if col < table[row].length && table[row][col] != null. But
that didnt work, how can I do this?

Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

Any advice would be gratefull.

Yep, use a ragged array ...

// note all zero based
int [] monthLengthNonLeapYear =
{31,28,31,30,31,30,31,31,30,31,30,31};

// Your day object .. array of arrays
Object [][] myCalendar = new Object[monthLengthNonLeapYear.length][];

// Create an array the right length for each month
for (int i = 0; i < monthLengthNonLeapYear.length; i++) {
myCalendar = new Object[monthLengthNonLeapYear];
}

// Play with it
for (int i = 0; i < myCalendar.length; i++) {
System.out.print("Month:"+i+" Days:");
for (int j = 0; j < myCalendar.length; j++) {
System.out.print(j+" ");
}
System.out.println();
}
 
J

Juggernaut

I am trying to get this to work with your array:

for (int row=0; row < myCalendar.length; row++)

{

for (int col=0; col < myCalendar[row].length; col++)

myCalendar[row][col] = (int) (Math.random() * 100);

}

but its not working

Juggernaut said:
But do these arrays actually be filled with any info? Isnt it just the loop
posting numbers?

You see I want to fill these day arrays with values, random values (since I
later have to do some calculations with them).

So I want the array, with 365 "cells" of random values, to be calculated
with later.


Andrew Harker said:
Juggernaut said:
I have a two dimensional array, [12][31], and I have it made as a
calender where the days are filled with some information. But some
months, like february only has 28 days so I havent filled 29 30 and
31. But now I want to make some calculations on these numbers, and
divide them by all the days. But the problem is they also count the
array fields like february 29 , 30 and 31 wich is empty..I tried a for
loop where if col < table[row].length && table[row][col] != null. But
that didnt work, how can I do this?

Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

Any advice would be gratefull.

Yep, use a ragged array ...

// note all zero based
int [] monthLengthNonLeapYear =
{31,28,31,30,31,30,31,31,30,31,30,31};

// Your day object .. array of arrays
Object [][] myCalendar = new Object[monthLengthNonLeapYear.length][];

// Create an array the right length for each month
for (int i = 0; i < monthLengthNonLeapYear.length; i++) {
myCalendar = new Object[monthLengthNonLeapYear];
}

// Play with it
for (int i = 0; i < myCalendar.length; i++) {
System.out.print("Month:"+i+" Days:");
for (int j = 0; j < myCalendar.length; j++) {
System.out.print(j+" ");
}
System.out.println();
}
 
J

Juggernaut

Tried two random objects and both say, cant convert from int to double. How
do I work around this?

Juggernaut said:
I am trying to get this to work with your array:

for (int row=0; row < myCalendar.length; row++)

{

for (int col=0; col < myCalendar[row].length; col++)

myCalendar[row][col] = (int) (Math.random() * 100);

}

but its not working

Juggernaut said:
But do these arrays actually be filled with any info? Isnt it just the loop
posting numbers?

You see I want to fill these day arrays with values, random values
(since
I
later have to do some calculations with them).

So I want the array, with 365 "cells" of random values, to be calculated
with later.


Andrew Harker said:
Juggernaut wrote:

I have a two dimensional array, [12][31], and I have it made as a
calender where the days are filled with some information. But some
months, like february only has 28 days so I havent filled 29 30 and
31. But now I want to make some calculations on these numbers, and
divide them by all the days. But the problem is they also count the
array fields like february 29 , 30 and 31 wich is empty..I tried a for
loop where if col < table[row].length && table[row][col] != null. But
that didnt work, how can I do this?

Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

Any advice would be gratefull.

Yep, use a ragged array ...

// note all zero based
int [] monthLengthNonLeapYear =
{31,28,31,30,31,30,31,31,30,31,30,31};

// Your day object .. array of arrays
Object [][] myCalendar = new Object[monthLengthNonLeapYear.length][];

// Create an array the right length for each month
for (int i = 0; i < monthLengthNonLeapYear.length; i++) {
myCalendar = new Object[monthLengthNonLeapYear];
}

// Play with it
for (int i = 0; i < myCalendar.length; i++) {
System.out.print("Month:"+i+" Days:");
for (int j = 0; j < myCalendar.length; j++) {
System.out.print(j+" ");
}
System.out.println();
}

 
A

Andrew Harker

(Please don't top post it makes the 'conversation' hard
to follow - corrected. )

YES - but the array can be indexed as you requested. You
have to either create objects for each element or use a
primitive type like int.

ummm int[365] then :)
I am trying to get this to work with your array:

for (int row=0; row < myCalendar.length; row++)
{
for (int col=0; col < myCalendar[row].length; col++)
myCalendar[row][col] = (int) (Math.random() * 100);
}
but its not working

If you used my example you should replace 'Object' with
'int', then assign the value - it works fine then.

If you need help with a code section then it is a good
idea to see http://www.physci.org/codes/sscce.jsp and not
try to do it iteratively over the newsgroup :)
 
P

Phil Hanna

But do these arrays actually be filled with any info? Isnt it just the loop
posting numbers?

You see I want to fill these day arrays with values, random values (since I
later have to do some calculations with them).

So I want the array, with 365 "cells" of random values, to be calculated
with later.

You may not need an array. How about a Map, where the key is a Date
object? You can use java.util.Calendar to give you the next day given
a particular date.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top