We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to create a list of countries from a csv file, where countries usually appear more than once. Every time I'm running this part of the code I'm getting an error when trying to append a new country to the list:
Syntax error, insert ".class" to complete ArgumentList
on that line"
String[] destCountryArray = append(destCountryArray[],tempCountryHolder);
I'm almost 100% there's a smarter easier way to do this, but I'm really not sure why this one doesn't work.
void setup() {
size(640, 360);
table = loadTable("asylum_seekers.csv", "header");
destCountryArray = new String[0];
loadData();
String[] destCountryArray = append(destCountryArray, "israel");
for (TableRow row : table.rows()) {
String tempCountryHolder = row.getString("Country / territory of asylum/residence");
for (int i = 0; i < destCountryArray.length; i++) {
println("comparing " +tempCountryHolder +" with "+destCountryArray[i]);
if (tempCountryHolder == destCountryArray[i]) {
println("stop");
stop();
}
else{
if (i==destCountryArray.length-1) {
String[] destCountryArray = append(destCountryArray[],tempCountryHolder);
println("assigning "+destCountryArray.length);
}
}
}
}
}
Answers
Read the reference for append
You need to cast String[]
Btw also look at HashMap in the reference which is cool when you don’t want duplicates
@Chrisir can you explain what do you mean by cast? I don't really see the difference between what I did here and the examples at the reference
Tip: Processing's IDE got an auto-format feature by hitting CTRL+T. *-:)
String[] destCountryArray = append(destCountryArray[], tempCountryHolder);
Why are you using an empty array access
[]
atdestCountryArray[],
? :-\"Either specify an integer value for its index, or just pass the variable w/o the array access operator. :-@
@GoToLoop I'm getting a
The local variable destCountryArray may have not been initialized
Basically you're attempting to access the local variable destCountryArray (assigning an array to it) BEFORE it is declared! #-o
IS THat your entire code?
Move the word String[] from line 7 to 5
Or is it defined before setup ()
I think it's declared before "setup()": :/
So why do you RE-declare it later within setup() as a local variable? :-&
By doing so, the local RE-declaration ends up overshadowing the global 1 within setup()'s scope. :-SS
So, no String[] in setup ()
Also look at HashMap
Please post the first 20 lines of your csv formatted as code so we can have a look
OK thank you I got that part sorted out
But it's still behaving strangely. It saves some countries twice or more even after noticing they already exist in the array, and even stranger - it doesn't ever get to start draw();
Please post the first 20 lines of your csv formatted as code so we can have a look
either turn to HashMap
OR write a function
and use it in setup()
some adjustments necessary, parameters for countryExists / not tested
CSV :)
I'm trying to avoid using Java libraries as (as far as I know) they cannot be used in processing.js
I hope to implement the code in some interactive website.
great
did you get my function to work?
Not yet it needs some work. As it's very late here I'll continue this in a few hours.
post your entire code then
http://ProcessingJS.org/reference/
Thank you
Found the source of the problem: I was trying to compare two strings
tempCountryHolder == destCountryArray[i]
that created false negatives because it didn't compare the content of the strings.changed the code to
tempCountryHolder.equals(destCountryArray[i]) == true
now it works fine
in this expression the
==true
is not needed