I am fairly new to programming, and I'm trying to do some work with arrays, but I'm getting an error that I don't know how to fix. Any help would be great!
Error: 1084: Syntax error: expecting colon before leftbracket.
Source: hockeyPP({hockeyPlayers[i]});
Error: 1084: Syntax error: expecting identifier before rightbrace.
Source: hockeyPP({hockeyPlayers[i]});
function eliminateAbsentees():void{
for(var i:int=0; i<=hockeyPlayers.length; i++){
if(hockeyPlayers[i].attendance==true){
hockeyPP.push({hockeyPlayers[i]});
}
}
}
remove { and } surrounding hockeyPlayers[i]. Why you want to used it in this way?
function eliminateAbsentees():void{
for(var i:int = 0; i <= hockeyPlayers.length; i++){
if(hockeyPlayers[i].attendance == true){
hockeyPP.push(hockeyPlayers[i]);
}
}
}
As mentioned by Azzy Elvul, your issue was the curly brackets ( "{}" ) around the array item. You'll see curly brackets in a few places:
Function Declarations
Object Declarations
Class Declarations
Loops
Conditionals
I think there is one more, but that is what I came up with off the top of my head. Basically, when you tried to use this line:
hockeyPP.push({hockeyPlayers[i]});
you tried to declare hockeyPlayers[i] as a new Object (the most basic class in ActionScript, and most languages). You can instantiate the Object class by two ways:
var obj:Object = new Object(); and
var obj:Object = {};
You tried to do the second one, the lazy instantiation. So you tried to declare an object with a property of hockeyPlayers[i] without associating a value with it (the basis of all OOP is property:value pairs).
As that first error said, you are missing a colon for that type of instantiation. If you were to try
hockeyPP.push({hockeyPlayers[i] : null}); //null is what an object is when it has no value
you would not have gotten any errors, as that is the correct way to instantiate an Object. For your needs, though, you just want to push an item from one array to another array. So you do
array2.push( array1[ selectedIndex ] );
I would definitely give the LiveDocs some reading. They can seem daunting, but they are incredibly well written and easy to understand once you start going through them.
LiveDocs - Array
LiveDocs - Object
Related
Please forgive my terminology, Im not educated on the proper.
Lets say I have multiple movieclip variables
var rblock1:MovieClip = new Rblock();
var rblock2:MovieClip = new Rblock();
var rblock3:MovieClip = new Rblock();
var yblock1:MovieClip = new Yblock();
var yblock2:MovieClip = new Yblock();
var yblock3:MovieClip = new Yblock();
I have them added to an array
var blockarray:Array = new Array(rblock1, rblock2, rblock3, yblock1, yblock2, yblock3);
var block
I want to create a for loop with an if statement that triggers if a variable is Rblock and not Yblock, for example
for each (block in blockarray)
{
if (block==Rblock)
{
trace("rblock");
}
}
The issue is that obviously "if (block==Rblock)" doesnt work.
How should this be written?
You apparently want to check if a block is red or yellow by checking against its class name. You can do it with this:
if (block is Rblock) {...} // yes, red
I have figured out a work around not really a perfect solution, which will only work for certain scenarios...
if each class has a unique trait you can identify it that way, for example...
if all variables defined by the Rblock class are wider than the Yblock class you could say
if (block.width>x) { trace(Rblock); }
Like I said this is only a work around though and only works for movieclip variables defined by classes that are different, if anyone has the actual solution please post
I am converting an app written using Swing to JavaFX. I need an array of TextFields that I can manipulate collectively and as individual array members addressed by the array index. The following code works in Swing but I cannot find the equivalent in JavaFX, using TextField instead of JTextField. How can I achieve this in JavaFX?
private ArrayList<JTextField> fieldList= new ArrayList<JTextField>();
fieldList.add(fldCompletion);
fieldList.add(fldHrsToDate);
fieldList.add(fldHrsToComplete);
for(JTextField fl : fieldList) {
fl.setText("");
fl.setEnabled(true); //FX equivalent is setDisable(false)
}
fieldList.get(var).setText("");
I also don't understand your question because this part of the code should work in JavaFX in the same way as it works in Java(Swing) and you are not telling us what your actual problem is. So I just make a wild guess. Maybe you have just forgotten to add your text field to the scene graph too and therefore your are seeing nothing.
Apologies - it's my first post.
I can instantiate the ArrayList -
private ArrayList<TextField> fieldList = new ArrayList<TextField>();
but when I try to add a TextField object to the array I get syntax errors:
fieldList.add(fldCompletion);
Multiple markers at this line
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error on token ".", # expected after this token
- Syntax error, insert "SimpleName" to complete QualifiedName
- Syntax error, insert "Identifier (" to complete
MethodHeaderName
This particular field is declared thus:
#FXML private TextField fldCompletion;
This will do just fine
TextField[] txt = new TextField[beanFields.length];
for (int i=0; i<=beanFields.length-1; i++) {
TextField textField = new TextField();
txt[i] = textField;
textField.setPrefWidth(200);
textField.setPrefHeight(32);
}
while playing around with typescript I ran into then following interesting behavior:
class ExtArray<U> extends Array<U> {
constructor(...args : U[]) {
super(...args);
}
public contains(element : U) : boolean {
var i = this.indexOf(element);
return i !== -1;
}
}
var test : ExtArray<string> = new ExtArray("a", "b", "c");
test.push("y");
console.log(test.length); // 1
console.log(test[0]); // y
console.log(test[1]); // undefined
console.log("Has a: " + test.contains("a")); // Has a: false
console.log("Has y: " + test.contains("y")); // Has y : true
I've added the output of the console.log statements as comments.
See this typescript playground for an executable example and the javascript code.
As you can see it seems as if the elements passed to the constructor are not added to the array.
The section about extending expression in Whats new in Typescript suggests that it should be possible to extend the native Array type like that in typescript 1.6.
Also I didn't find anything in the typescript language reference,
that explains this behavior.
Most of the other questions about extending Arrays I found here are at least one year old and usually talk about a pre-1.0 version of typescript and therefore suggest to set up the prototype chain directly.
I seriously don't see what is going wrong here and I'm starting to suspect a typescript bug.
Or at least some kind of undocumented restriction for extending Arrays.
What goes wrong here?
It's a little easier to understand what's going on if you JSON.stringify() your object:
var test : ExtArray<string> = new ExtArray("a", "b", "c");
test.push("y");
// outputs {"0":"y","length":1}
document.writeln(JSON.stringify(test));
If you instead new-up a native Array, the resulting object is quite a bit different:
var test : Array<string> = new Array("a", "b", "c");
test.push("y");
// outputs ["a","b","c","y"]
document.writeln(JSON.stringify(test));
I agree with you that the documentation seems to imply that the subclass's constructor should behave the way you're expecting. Even stranger, I seem to get inconsistent results when testing whether or not the subclass is an Array using the methods described here:
test.constructor === Array // false
test instanceof Array // true
Array.isArray(test) // false
I would suggest opening an issue on the TypeScript GitHub repository. Even if this is the expected behavior, the official documentation is misleading and should clarify what exactly is expected when native objects are subclassed.
I've used this website for many things and found a lot of useful information that has helped me create a randomized quiz mostly. I'm trying to make the code as efficient as possible and that has led me to this error.
I have created an array which uses the .push function to store existing buttons on the stage into the array for future use. The code shown below sets the label of each button correctly.
_buttons[0].label = xmlData.difficulty1.questions[num2].op1.text();
_buttons[1].label = xmlData.difficulty1.questions[num2].op2.text();
_buttons[2].label = xmlData.difficulty1.questions[num2].op3.text();
_buttons[3].label = xmlData.difficulty1.questions[num2].op4.text();
So naturally I wanted to make this a little more efficient and put these in a loop. Based on the logic this SHOULD work but doesn't.
for (var i:Number = 0; i < 4; i++)
{
_buttons[i].label = xmlData.difficulty1.questions[num2].op[i+1].text();
}
This code should simply increment the array counter of _buttons and set the label for each. I mean it's simply the first set of code in a for loop right? However when I run it I get the following error: TypeError: Error #1010: A term is undefined and has no properties.
Now I know for sure that the first set of code works as I have tested it repeatedly but as soon as I decide to put it into that for loop, it fails. Could anyone explain why? Perhaps it's a limitation of the language itself? Maybe I'm missing a command?
Try :
for (var i:Number = 0; i < 4; i++)
{
_buttons[i].label = xmlData.difficulty1.questions[num2]['op'+(i+1)].text();
}
.op[i+1] tries to access a property called 1 under a field called op instead of op1
I have created an array of objects and I would like assign a property value in a vector operation without using a for loop. Unfortunately I get an error.
A simplified example of the problem.
classdef clsMyClass < handle
properties
dblMyProperty1
end
methods
function obj = clsMyClass()
end
end
end
And when running
vecMyArray = clsMyClass.empty(100,0);
vecMyArray(100) = clsMyClass;
vecMyArray.dblMyProperty1 = 1:100;
We get the following error:
??? Incorrect number of right hand side elements in dot name
assignment. Missing [] around left hand side is a likely cause.
Any help would be appreciated.
I see what you're trying to do now. Use disperse from the MATLAB File Exchange:
>> [vecMyArray.dblMyProperty1] = disperse(1:100);
>> vecMyArray(1).dblMyProperty1
ans =
1
>> vecMyArray(10).dblMyProperty1
ans =
10
You can use the deal function for exactly this purpose:
[vecMyArray.dblMyProperty1] = deal(1:100);
See: http://www.mathworks.com/company/newsletters/articles/whats-the-big-deal.html
Edit: No you can't, actually; that'll set them to all be the vector 1:100.
I think you'll find your answer here in "Struct array errors." Even though this is a class, similar rules apply.
Unfortunately missing [] is not the cause, since adding them causes more errors. The cause is that you cannot assign the same value to all fields of the same name at once, you must do it one at a time, as in the following code:
So you'll need:
for ii=1:100
vecMyArray(ii).dblMyProperty1 = ii;
end
I know it's not satisfying, but I think it at least helps us to definitively understand this error.