append() creates errors

edited March 2018 in JavaScript Mode

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

  • edited March 2018

    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 [] at destCountryArray[],? :-\"

    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

    void setup() {
      size(640, 360);
      table = loadTable("asylum_seekers.csv", "header");
      loadData();
      destCountryArray = new String[0];
    
      String[] destCountryArray = (String[]) 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) {
              destCountryArray = (String[]) append(destCountryArray, tempCountryHolder);
              println("assigning "+destCountryArray.length);
            }
          }
        }
      }
    }
    
  • edited March 2018
    destCountryArray = new String[0];
    
    String[] destCountryArray = (String[]) append(destCountryArray, "israel");
    

    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()": :/

    Table table;
    int k = 0;
    int i = 0;
    String[] destCountryArray;
    
    String destCountry = "";
    String prevDestCountry = "";
    
    void setup() {
      size(640, 360);
      table = loadTable("asylum_seekers.csv", "header");
      loadData();
      destCountryArray = new String[0];
    
      String[] destCountryArray = (String[]) 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) {
              destCountryArray = (String[]) append(destCountryArray, tempCountryHolder);
              println("assigning "+destCountryArray.length);
            }
          }
        }
      }
    }
    
  • 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();

    Table table;
    int k = 0;
    String[] destCountryArray;
    
    String destCountry = "";
    String prevDestCountry = "";
    
    void setup() {
      size(640, 360);
      table = loadTable("asylum_seekers.csv", "header");
      loadData();
      destCountryArray = new String[0];
    
      destCountryArray = (String[]) 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("stopping : "+ tempCountryHolder + " = " + destCountryArray[i]);
            stop();
          } else {
            if (i==destCountryArray.length-1) {
              destCountryArray = (String[]) append(destCountryArray, tempCountryHolder);
              println("assigning "+tempCountryHolder+" to " +destCountryArray.length);
            }
          }
        }
      }
    }
    
    void draw() {
      background(255);
    
      println (destCountryArray[k]+k);
      k++;
    }
    
  • Please post the first 20 lines of your csv formatted as code so we can have a look

  • edited March 2018

    either turn to HashMap

    OR write a function

    boolean countryExists() {
    
        for (int i = 0; i < destCountryArray.length; i++) {
              //println("comparing " +tempCountryHolder +" with "+destCountryArray[i]);
              if (tempCountryHolder == destCountryArray[i]) {
                println("stopping : "+ tempCountryHolder + " = " + destCountryArray[i]);
                return true; // it exists 
              } //if
        } //for
        return false ; // not found 
    }//func
    

    and use it in setup()

    some adjustments necessary, parameters for countryExists / not tested

  • CSV :)

    Year    Country / territory of asylum/residence Origin  RSD procedure type / level  Tota pending start-year of which UNHCR-assisted(start-year) Applied during year decisions_recognized    decisions_other Rejected    Otherwise closed    Total decisions Total pending end-year  of which UNHCR-assisted(end-year)
    2000    Zimbabwe    Afghanistan G / FI  0   0   5   5   0   0   0   5   0   0
    2000    South Africa    Afghanistan G / FI  8   1   0   0   0   0   0       8   0
    2000    Uzbekistan  Afghanistan U / FI  265 265 2156    747 0   112 327 1186    1235    1235
    2000    United States of America    Afghanistan G / EO  196 0   225 151 0   31  68  250 171 0
    2000    United States of America    Afghanistan G / IN  193 0   218 182 0   51  40  273 150 0
    2000    Ukraine Afghanistan G / FI  40  0   662 275 0   412 0   687 23  0
    2000    Turkey  Afghanistan U / FI  67  67  81  29  0   24  49  102 46  46
    2000    Turkmenistan    Afghanistan U / FI  416 416 169 126 0   121 210 457 128 128
    2000    Tajikistan  Afghanistan G / FI  2172    30  165 112 0   0   1992    2104    233 40
    2000    Thailand    Afghanistan U / AR  0   0   2   1   0   1   0   2   0   0
    2000    Thailand    Afghanistan U / FI  5   0   25  14  1   2   4   21  9   5
    2000    Syrian Arab Rep.    Afghanistan U / FI  311 0   94  62  0   138 180 380 25  0
    2000    Sweden  Afghanistan G / AR  0   0   0   0   44  124 1   169 0   0
    2000    Sweden  Afghanistan G / FI  0   0   374 33  563 78  13  687 0   0
    2000    Slovenia    Afghanistan G / FI  4   0   247 0   0   2   11  13  238 0
    2000    Slovakia    Afghanistan G / FI  90  0   624 5   0   28  559 592 122 0
    2000    Serbia and Kosovo (S/RES/1244 (1999))   Afghanistan U / FI  0   0   7   0   0   5   2   7   0   0
    2000    Russian Federation  Afghanistan G / FI  1009    0   1088    160 0   1317    0   1477    620 0
    2000    Romania Afghanistan G / FI  10  10  282 27  35  191 0   253 0   0
    2000    Portugal    Afghanistan G / FI  0   0   4   0   1   1   0   2   0   0
    
  • 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

  • ... they cannot be used in processing.js.

    http://ProcessingJS.org/reference/

  • Answer ✓

    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

Sign In or Register to comment.