Array is only initializing the first value

To put simply, I am making a program that will take in a string of unpredictable integers with brackets on the ends and each integer separated by commas for example [132, 1, 46, 98] and manipulate that string so that it will have four integers in a int array and can be used throughout the rest of the program. The problem is when I run it it only saves the first values and the rest are zeros. Im guessing the problem has something to do with saving the variable to the array.

//Test 1 string to var 
String Test1 = "[ 132, 1, 46, 98 ]";

void setup() {
  Serial.begin(9600);
  Serial.println("Test");
 // Test1.replace(" ","");
  Test1.replace("[","");
  Test1.replace("]","");
  Serial.println(Test1);
  int NewTest1[4] = {Test1.toInt()};
  
  for(int i = 0; i < 4; i++){
    Serial.println(NewTest1[i]);
      
    }
}

void loop() {
  
  
}

For output

Test
 132, 1, 46, 98
132
0
0
0

I'm pretty new to Arduino so if you can think of an easier way to do this id be happy to hear.

Thanks all for helping

Why are you declaring an array of type String and then filling it with unsigned ints?

Better to declare an array of unsigned int and populate it thus:

unsigned int Test1[] = { 132, 1, 46, 98 };

In fact looking back at your code again, you’re not even declaring an array for Test1, it’s just a string as it’s all within the double quotes. You don’t need all the lines that manipulate the value of Test1, just ensure you declare an array as you intended.

Unfortunately for you, String.toInt() only converts the first integer it sees and can only return a single value. Is your input coming from Serial? If so you might want to use Serial.parseInt() to get the value of each integer:

  int NewTest1[4];
 
  for(int i = 0; i < 4; i++)
  {
    NewTest1[i] = Serial.parseInt();  // Oops.. originally written as ".toInt()"
    Serial.println(NewTest1[i]);
  }

Yup through serial, ill look up more on Serial.parseInt(), thanks!

You might get some ideas from Robin's updated Serial Input Basics thread.

It uses character arrays instead of String (capital S).