Question about For Loop

Hello Everyone,

I have a below piece of code which is basically deciding the length phone number digits and then entering phone number based on that length.

int Select_phonestring_lenght = new Random().nextInt((10 - 1) + 1) + 1

for (int Enter_Phone_Number = 0; Enter_Phone_Number < Select_phonestring_lenght; Enter_Phone_Number ++) {
	
	def phonestring = org.apache.commons.lang.RandomStringUtils.randomNumeric(1)
	
	WebUI.sendKeys(findTestObject('Customer_Information_Page/Field_Phone'), phonestring)

}

Within this for loop, I also want to save the value of variable phonestring in another variable but I am not sure how to do that because according to my understanding the second time the loop will run it would delete the value already stored in that variable.

The reason I want to store the phonestring in a variable is because in the next steps I am trying to concatenate all those variables and match with the number that was expected.

int Select_phonestring_lenght = new Random().nextInt((10 - 1) + 1) + 1

def savedPhonestring

for (int Enter_Phone_Number = 0; Enter_Phone_Number < Select_phonestring_lenght; Enter_Phone_Number ++) {
	
	def phonestring = org.apache.commons.lang.RandomStringUtils.randomNumeric(1)
	
	WebUI.sendKeys(findTestObject('Customer_Information_Page/Field_Phone'), phonestring)

    savedPhonestring += phonestring

}

System.out.println(savedPhonestring)

@Brandon_Hein Thanks the solution worked the only issue is it is adding null to the beginning of the savedPhonestring

e.g : null0638129

@Brandon_Hein I removed the null using this way. Is this way correct

String StrWithNull_ = savedPhonestringwithNull

String SavedPhonestring = StrWithNull_.replaceAll('[null]', '')

WebUI.println('The Phone String should be  : ' + SavedPhonestring)

It is working fine.

1 Like