Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Updated problem
Source Link

Array is randomly deleting Can't assign elements to index in array

I'm trying to create an array of strings. I'm building each individual string by parsing a json file and getting the strings between '{'s and '}'s. However, for some reason, my array is loosing values and only assigning one index with the string i want. This is my code:

  String day = "";
  String Combo[] = {"0","0","0","0","0","0","0","0"};
  int x = 2;
  int number = 0;
  
  /* Read data until either the connection is closed, or the idle timeout is reached. */ 
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    /* Reads one character at a time*/
    while (www.available()) {
      char sym = www.read();
     
      if (sym == '{') {
        x = 1;
      }
      if (x==1){
        if (!day.concat(sym)) {
          //Serial.println("concat() error!");
        }
      }
      if (x==1 && sym == '}'){
        x=0;
        Serial.println(number);
      }
      
      if ((number > 0) && (number < 9) && (x==0)) {
        Combo[number-1] = day;
        Serial.print(day);
        forCombo[number-1] (int= jday;
 = 0; j 
 < 7; j++) {
    Serial.println(F("-------------------------------------"));
        Serial.printprintln(Combo[j]Combo[number-1]);
     
    }
        Serial.println(F("-------------------------------------"));
        Serial.println(F("-------------------------------------"));
        day = "";
        number +=1;
        x = 2;
      } else if (number ==0 && x ==0){
        day = "";
        number +=1;
        x = 2;
      }
      lastRead = millis();
  
    }

  }
0{
1    "firstString"
000000    }-------------------------------------
2
-------------------------------------
-------------------------------------
{
    "secondString"
    "firstString"}-------------------------------------
{
    "secondString"
    }00000 
-------------------------------------
concat() error! ..... concat() error!
3-------------------------------------
{
        "firstString""thirdString"
        }0000-------------------------------------
concat() error! ..... concat() error!
4-------------------------------------
-------------------------------------
{
        "firstString""fourthString"
        }000-------------------------------------
concat() error! ..... concat() error!
5-------------------------------------
-------------------------------------
{
        "firstString""fifthString"
        }00-------------------------------------
concat() error! ..... concat() error!
6-------------------------------------
-------------------------------------
{
        "firstString""sixthString"
        }0-------------------------------------
concat() error! ..... concat() error!
7-------------------------------------
-------------------------------------
{
        "firstString""seventhString"
        }-------------------------------------
concat() error! ..... concat() error!
8-------------------------------------
-------------------------------------
{
    "eighthString"
    "firstString"}-------------------------------------

-------------------------------------
-------------------------------------
{
    "secondString"
    }-------------------------------------
9
9
9

First, "firstString" is the only stringBased on this output I know that day is being constructed correctly but for some reason isn't assigned to an index (this index changes randomly) in Combo, and second, Combo looses elements (the 00000s decrease every iterationexcept "secondString").

Does Does anyone know what's going on?

Ideally Combo would equal: {"firstString", "secondString""secondString", "thirdString", ... "seventhString""eighthString"}

EDIT: It turns out that "day.concat(sym)" fails sometimes and randomly. Does anyone know why?

Array is randomly deleting elements

I'm trying to create an array of strings. I'm building each individual string by parsing a json file and getting the strings between '{'s and '}'s. However, for some reason, my array is loosing values and only assigning one index with the string i want. This is my code:

  String day = "";
  String Combo[] = {"0","0","0","0","0","0","0","0"};
  int x = 2;
  int number = 0;
  
  /* Read data until either the connection is closed, or the idle timeout is reached. */ 
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    /* Reads one character at a time*/
    while (www.available()) {
      char sym = www.read();
     
      if (sym == '{') {
        x = 1;
      }
      if (x==1){
        if (!day.concat(sym)) {
          Serial.println("concat() error!");
        }
      }
      if (x==1 && sym == '}'){
        x=0;
        Serial.println(number);
      }
      
      if ((number > 0) && (number < 9) && (x==0)) {
        Combo[number-1] = day;
        
        for (int j = 0; j < 7; j++) {
          Serial.print(Combo[j]);
        }
        Serial.println(F("-------------------------------------"));
        day = "";
        number +=1;
        x = 2;
      } else if (number ==0 && x ==0){
        day = "";
        number +=1;
        x = 2;
      }
      lastRead = millis();
  
    }

  }
0
1
000000-------------------------------------
2
{
        "firstString"
        }00000-------------------------------------
concat() error! ..... concat() error!
3
{
        "firstString"
        }0000-------------------------------------
concat() error! ..... concat() error!
4
{
        "firstString"
        }000-------------------------------------
concat() error! ..... concat() error!
5
{
        "firstString"
        }00-------------------------------------
concat() error! ..... concat() error!
6
{
        "firstString"
        }0-------------------------------------
concat() error! ..... concat() error!
7
{
        "firstString"
        }-------------------------------------
concat() error! ..... concat() error!
8
{
        "firstString"
        }-------------------------------------
9
9
9

First, "firstString" is the only string that is assigned to an index (this index changes randomly) in Combo, and second, Combo looses elements (the 00000s decrease every iteration).

Does anyone know what's going on?

Ideally Combo would equal: {"firstString", "secondString", "thirdString", ... "seventhString"}

EDIT: It turns out that "day.concat(sym)" fails sometimes and randomly. Does anyone know why?

Can't assign elements to index in array

I'm trying to create an array of strings. I'm building each individual string by parsing a json file and getting the strings between '{'s and '}'s. However, for some reason, my array is only assigning one index with the string i want. This is my code:

  String day = "";
  String Combo[] = {"0","0","0","0","0","0","0","0"};
  int x = 2;
  int number = 0;
  
  /* Read data until either the connection is closed, or the idle timeout is reached. */ 
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    /* Reads one character at a time*/
    while (www.available()) {
      char sym = www.read();
     
      if (sym == '{') {
        x = 1;
      }
      if (x==1){
        if (!day.concat(sym)) {
          //Serial.println("concat() error!");
        }
      }
      if (x==1 && sym == '}'){
        x=0;
        Serial.println(number);
      }
      
      if ((number > 0) && (number < 9) && (x==0)) {
        Combo[number-1] = day;
        Serial.print(day);
        Combo[number-1] = day;
    
        Serial.println(F("-------------------------------------"));
        Serial.println(Combo[number-1]);
     
    
        Serial.println(F("-------------------------------------"));
        Serial.println(F("-------------------------------------"));
        day = "";
        number +=1;
        x = 2;
      } else if (number ==0 && x ==0){
        day = "";
        number +=1;
        x = 2;
      }
      lastRead = millis();
  
    }

  }
{
    "firstString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "secondString"
    }-------------------------------------
{
    "secondString"
    } 
-------------------------------------
-------------------------------------
{
    "thirdString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "fourthString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "fifthString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "sixthString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "seventhString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "eighthString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "secondString"
    }-------------------------------------

Based on this output I know that day is being constructed correctly but for some reason isn't assigned to an index in Combo (except "secondString") Does anyone know what's going on?

Ideally Combo would equal: {"secondString", "thirdString", ... "eighthString"}

added 258 characters in body
Source Link
0
1
000000-------------------------------------
2
{
        "firstString"
        }00000-------------------------------------
concat() error! ..... concat() error!
3
{
        "firstString"
        }0000-------------------------------------
concat() error! ..... concat() error!
4
{
        "firstString"
        }000-------------------------------------
concat() error! ..... concat() error!
5
{
        "firstString"
        }00-------------------------------------
concat() error! ..... concat() error!
6
{
        "firstString"
        }0-------------------------------------
concat() error! ..... concat() error!
7
{
        "firstString"
        }-------------------------------------
concat() error! ..... concat() error!
8
{
        "firstString"
        }-------------------------------------
9
9
9
0
1
000000-------------------------------------
2
{
        "firstString"
        }00000-------------------------------------
3
{
        "firstString"
        }0000-------------------------------------
4
{
        "firstString"
        }000-------------------------------------
5
{
        "firstString"
        }00-------------------------------------
6
{
        "firstString"
        }0-------------------------------------
7
{
        "firstString"
        }-------------------------------------
8
{
        "firstString"
        }-------------------------------------
9
9
9
0
1
000000-------------------------------------
2
{
        "firstString"
        }00000-------------------------------------
concat() error! ..... concat() error!
3
{
        "firstString"
        }0000-------------------------------------
concat() error! ..... concat() error!
4
{
        "firstString"
        }000-------------------------------------
concat() error! ..... concat() error!
5
{
        "firstString"
        }00-------------------------------------
concat() error! ..... concat() error!
6
{
        "firstString"
        }0-------------------------------------
concat() error! ..... concat() error!
7
{
        "firstString"
        }-------------------------------------
concat() error! ..... concat() error!
8
{
        "firstString"
        }-------------------------------------
9
9
9
added 169 characters in body
Source Link
  String day = "";
  String Combo[] = {"0","0","0","0","0","0","0","0"};
  int x = 2;
  int number = 0;
  
  /* Read data until either the connection is closed, or the idle timeout is reached. */ 
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    /* Reads one character at a time*/
    while (www.available()) {
      char sym = www.read();
     
      if (sym == '{') {
        x = 1;
      }
      if (x==1){
        if (!day.concat(sym)) {
          Serial.println("concat() error!");
        }
      }
      if (x==1 && sym == '}'){
        x=0;
        Serial.println(number);
      }
      
      if ((number > 0) && (number < 9) && (x==0)) {
        Combo[number-1] = day;
        
        for (int j = 0; j < 7; j++) {
          Serial.print(Combo[j]);
        }
        Serial.println(F("-------------------------------------"));
        day = "";
        number +=1;
        x = 2;
      } else if (number ==0 && x ==0){
        day = "";
        number +=1;
        x = 2;
      }
      lastRead = millis();
  
    }

  }

Ideally Combo would equal: {"firstString", "secondString", "thirdString", ... "seventhString"}

EDIT: It turns out that "day.concat(sym)" fails sometimes and randomly. Does anyone know why?

  String day = "";
  String Combo[] = {"0","0","0","0","0","0","0","0"};
  int x = 2;
  int number = 0;
  
  /* Read data until either the connection is closed, or the idle timeout is reached. */ 
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    /* Reads one character at a time*/
    while (www.available()) {
      char sym = www.read();
     
      if (sym == '{') {
        x = 1;
      }
      if (x==1){
        day.concat(sym);
      }
      if (x==1 && sym == '}'){
        x=0;
        Serial.println(number);
      }
      
      if ((number > 0) && (number < 9) && (x==0)) {
        Combo[number-1] = day;
        
        for (int j = 0; j < 7; j++) {
          Serial.print(Combo[j]);
        }
        Serial.println(F("-------------------------------------"));
        day = "";
        number +=1;
        x = 2;
      } else if (number ==0 && x ==0){
        day = "";
        number +=1;
        x = 2;
      }
      lastRead = millis();
  
    }

  }

Ideally Combo would equal: {"firstString", "secondString", "thirdString", ... "seventhString"}

  String day = "";
  String Combo[] = {"0","0","0","0","0","0","0","0"};
  int x = 2;
  int number = 0;
  
  /* Read data until either the connection is closed, or the idle timeout is reached. */ 
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    /* Reads one character at a time*/
    while (www.available()) {
      char sym = www.read();
     
      if (sym == '{') {
        x = 1;
      }
      if (x==1){
        if (!day.concat(sym)) {
          Serial.println("concat() error!");
        }
      }
      if (x==1 && sym == '}'){
        x=0;
        Serial.println(number);
      }
      
      if ((number > 0) && (number < 9) && (x==0)) {
        Combo[number-1] = day;
        
        for (int j = 0; j < 7; j++) {
          Serial.print(Combo[j]);
        }
        Serial.println(F("-------------------------------------"));
        day = "";
        number +=1;
        x = 2;
      } else if (number ==0 && x ==0){
        day = "";
        number +=1;
        x = 2;
      }
      lastRead = millis();
  
    }

  }

Ideally Combo would equal: {"firstString", "secondString", "thirdString", ... "seventhString"}

EDIT: It turns out that "day.concat(sym)" fails sometimes and randomly. Does anyone know why?

Source Link
Loading