Im trying to make a multidimensional array to save the data but I obtain an error ("TypeError: Error #1010: A term is undefined and has no properties.").
var cols:int = sheet.cols;
var rows:int = sheet.values.length;
var ridx:int = 0;
var cidx:int = 0;
var out:Array = new Array();
var i:int;
for (i=0; i < cols; i++) {
out[i] = new Array();
var j:int;
for (j=0; j < rows; j++) {
out[ridx][cidx] = sheet.getCell(j, i).value;
ridx++;
if(ridx >= rows) {
cidx++;
ridx = 0;
}
}
}
what am I doing wrong?
Thanks in advance!
EDIT:
Now I have this code:
for (var i:int = 0; i < cols; i++) {
out[i] = new Array();
for (var j:int = 0; j < rows; j++) {
out[j][i] = sheet.getCell(j, i).value; //IM GETTING THE ERROR HERE
}
}
You're trying to assign a cell value to a nonexistent location in your output array.
In the first i loop, you assign out[0] = new Array();, then in the j loop, you'll set out[0][0] = sheet.getCell(0,0).value;
In the second j loop, you'll set out[1][0] = sheet.getCell(1,0).value;
In this case, sheet.getCell(1,0) may return null, but whether it does or not, out[1] is null, so your code translates to null[0] = ... -- which will trigger the error you're seeing.
You can probably fix this by swapping i and j in your inner loop.
Related
I am trying to make array of strings each representing one card of a poker deck from 2 strings (ranks, colors). If I try print card immidietly after assignment it's ok but if I try it after all assignments nothing happend.
My "code":
int main(void)
{
char rank[] = "23456789TJQKA";
char color[] = "cdhs";
char deck[52][3];
int k = 0;
for (int i = 0; i < 13; i++) {
for(int j = 0; j < 4; j++) {
deck[k][0] = rank[i];
deck[k][1] = color[j];
deck[k][2] = 0;
k++;
printf("%s\n",deck[k-1]); // this print works
}
}
printf("%s\n",deck[0]); //this does nothing (even if I change index)
//-------------------------- here I am trying make all possible pairs but deck is now empty :(
k = 0;
char allPairs[1327][5];
for (int i = 0; i < 51; i++) {
for (int j = 0; j < 52; j++) { //**edit** - thanks ;)
allPairs[k][0] = deck[i][0];
allPairs[k][1] = deck[i][1];
allPairs[k][2] = deck[j][0];
allPairs[k][3] = deck[j][1];
allPairs[k][4] = 0;
k++;
}
}
}
All seems to work now thanks guys!
What you need to do is replace i++ with j++ in the following statement
for (int j = 0; j < 3; i++)
and also comment out the following line as it is printing 2c again:
printf("%s\n",deck[0]); //this does nothing (even if I change index)
How do i execute an IF statment inside a double for loop, checking if an object in an array equals the selectedItem.label? Here is my try! (didn't work)
function klikkA(evt:Event):void{
for( var j:int = 0; j < 4; j++)
{
for (var k:int = 0; k < 8; k++)
{
if (listeA.selectedItem.label != myArray[j][k])
{
continue;
}
else if(listeA.selectedItem.label == myArray[j][k])
{
txtFlagg.text = myArray[j][k];
break;
}
}
}
}
The break statement alone will only break the inner loop.
As you can see in the documentation of break, you can specify a label
break [label]
It explains:
In nested loops, break only skips the rest of the immediate loop and does not break out of the entire series of nested loops. To break out of an entire series of nested loops, use label
There's additional documentation on the label keyword
It provides an example comparable to yours:
outerLoop: for (var i:int = 0; i < 10; i++) {
for (var j:int = 0; j < 10; j++) {
if ( (i == 8) && (j == 0)) {
break outerLoop;
}
trace(10 * i + j);
}
}
/*
1
2
...
79
*/
Since break can only jump out of single loop you need to either use flag or goto.
I'd use flag that outer loop(s) check as it generally more accepted practice:
var found = false;
for( var j:int = 0; !found && j < 4; j++)
{
for (var k:int = 0; k < 8; k++)
{
.... if(listeA.selectedItem.label == myArray[j][k])
{ ....
found = true;
break;
}
}
}
What i'm trying to accomplish is trying to compare two strings in an array and output the number of occurances for each into a new array. I.E. Given a set constant Array of test0,test1,test2, and another array of test1,test1,test2 I want to search through the array to give a desired output of 0,2,1.
This is my code thus far but I am still at a loss on how to accomplish this.
private function findVal(arr1: Array, arr2: Array): Array {
var tempNum: Array = new Array();
for (var i: int = 0; i < arr1.length; i++) {
for (var ii: int = 0; ii < arr2.length; ii++) {
if (arr1[i] == arr2[ii]) {
var num: int = 0;
//not sure where to go forward from here
}
}
}
return tempNum;
}
Any help would be greatly appreciated, thank you.
This code simply counts up the number of occurrences of a string, then stores that in the tempNum array, and when the searching through arr1 is complete, the tempNum array is returned to the caller.
private function findVal(arr1:Array, arr2:Array): Array {
var tempNum:Array = new Array(arr1.length);
for (var i:int = 0; i < arr1.length; i++) {
var num:int = 0;
for (var j:int = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
num++; // count up the number of occurrences as you iterate through arr2 and compare with arr1
}
}
tempNum[i] = num; // store the number of occurrences of string at index i of arr1 that occurred in arr2
}
return(tempNum);
}
I have created a 3 by 3 array of buttons but I am finding it hard to loop through each of the button, it won’t loop correctly and I don’t know where I have gone wrong. Below is what I have:
JButton[][] button = new JButton[3][3];
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
frame.setVisible(true);
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
button[i][j] = new JButton();
panel.add(button[i][j]);
}
}
Also, I would like them to be put in a 3 by 3 grid, instead of in a line.
Thanks in advance.
To put them in a 3 by 3 grid, you will need to add GridLayout to your panel, like so;
panel.setLayout(new GridLayout(3,3));
and also, you have made a mistake with the way you have used the for loop, instead of having
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
you need
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
Because you have used 2D array and created a 3 by 3 array, you need to put 3 in the for loop instead of 9.
So your final code will be something like
JButton[][] button = new JButton[3][3];
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
frame.setVisible(true);
frame.setSize(340,340);
panel.setLayout(new GridLayout(3,3));
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
button[i][j] = new JButton();
panel.add(button[i][j]);
}
}
Hope this helped
You are looping through as if you made a 9x9 2D array, but you made a 3x3.
trace(listOfCells)I don't understand how to do this. I currently have this code on the timeline:
var listOfCells:Array = new Array();
for (var i:int =0; i < stage.numChildren; i++)
{
listOfCells.push(stage.getChildAt(i));
}
trace(listOfCells)
How would one type a code that adds each item of a class to the array? Because when I trace the array, it only says [object MainTimeline] once, not twice (I have two identical movieclips on the stage).
The reason for all this is so I can compare each movieclip's x and y value to every other one, and then move toward each other.
Thanks in advance.
You are doing right. To access the objects inside the array, again you need to loop through it . Try this,
var listOfCells:Array = new Array();
for (var i:int =0; i < this.numChildren; i++)
{
listOfCells.push(this.getChildAt(i));
}
var len:int = listOfCells.length;
for (var j:int =0; j < len; j++)
{
trace(listOfCells[j].x);
}
or you can access the root of the application to get the elements like below,
var listOfCells:Array = new Array();
for (var i:int =0; i < (root as MovieClip).numChildren; i++)
{
listOfCells.push((root as MovieClip).getChildAt(i));
}
trace(listOfCells)
var len:int = listOfCells.length;
for (var j:int =0; j < len; j++)
{
trace(listOfCells[j].x);
}