Convert string to integer

Hello colleagues!

Have one question about converting. Have the following code

CustomKeywords.'demomysql.Demo.connectDB'('demo', 'demo', '3306', 'user', 'test')

String user_id = CustomKeywords.'demomysql.Demo.executeQuery'('SELECT user_id FROM contracts order by user_id desc;')

String contract_id = CustomKeywords.'demomysql.Demo.executeQuery'('SELECT id FROM contracts order by user_id desc;')

String summ_of_client_payments = CustomKeywords.'demomysql.Demo.executeQuery'('select  sum(LEFT(external_account, length(external_account) -3)) as external_account from transactions where contract_id = '+ contract_id +';') // equals 8000

String total_credit_summ = CustomKeywords.'demomysql.Demo.executeQuery'('select  sum(LEFT(internal_account, length(internal_account) -3)) as internal_account from transactions where contract_id = '+ contract_id +';') // equals 10021

a = asInteger(total_credit_summ)

b = asInteger(summ_of_client_payments)
asInteger (client_need_to_pay = ( a - b ))
println(client_need_to_pay)

//int client_need_to_pay = total_credit_summ - summ_of_client_paiments 

//println(client_need_to_pay) 
CustomKeywords.'demomysql.Demo.closeDatabaseConnection'()

when i execute this code have the following error:

2019-09-04 09:02:23.479 DEBUG testcase.Tarrif limit test case          - 8: Integer.parseInt(client_need_to_pay = a - b)
2019-09-04 09:02:23.491 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/Tarrif limit test case FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static java.lang.Integer.parseInt() is applicable for argument types: (java.lang.Integer) values: [2021]
Possible solutions: parseInt(java.lang.String), parseInt(java.lang.String, int), print(java.lang.Object), print(java.io.PrintWriter)
	at Tarrif limit test case.run(Tarrif limit test case:40)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1567576941377.run(TempTestCase1567576941377.groovy:21)

Maybe someone can help with converting String to integer, what methods do you use in case when you need to convert for example string ‘123’ to integer 123 and after this make some arithmetic manipulations with it. if i make my select something like this :

int total_credit_summ = CustomKeywords.'demomysql.Demo.executeQuery'('select  sum(LEFT(internal_account, length(internal_account) -3)) as internal_account from transactions where contract_id = '+ contract_id +';') // equals 10021

i also get errors

2019-09-04 09:49:26.888 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/Tarrif limit test case FAILED.
Reason:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '8000' with class 'java.lang.String' to class 'int'

Hi

a and b are integers, you don’t need to parse client_need_to_pay as integer.

You can use

a = total_credit_summ as Integer
b = summ_of_client_payments as Integer
client_need_to_pay = a - b

i tried use

a = total_credit_summ
b = summ_of_client_payments
client_need_to_pay = ( a - b )
println(client_need_to_pay)

no errors but i get client_need_to_pay = 10021
but i have a = 10021 and b = 8000 so a - b should be 2021 something interesting =))

console log:

2019-09-04 10:27:44.103 DEBUG testcase.Tarrif limit test case          - 6: a = total_credit_summ
2019-09-04 10:27:44.104 DEBUG testcase.Tarrif limit test case          - 7: b = summ_of_client_payments
2019-09-04 10:27:44.105 DEBUG testcase.Tarrif limit test case          - 8: client_need_to_pay = a - b
2019-09-04 10:27:44.106 DEBUG testcase.Tarrif limit test case          - 9: println(client_need_to_pay)
10021

i find the solution :slight_smile:

def  summ_of_client_payments = CustomKeywords.'demomysql.Demo.executeQuery'('select  sum(LEFT(external_account, length(external_account) -3)) as external_account from u_transactions where contract_id = '+ contract_id +';') // equals 8000

def  total_credit_summ = CustomKeywords.'demomysql.Demo.executeQuery'('select  sum(LEFT(internal_account, length(internal_account) -3)) as internal_account from u_transactions where contract_id = '+ contract_id +';') // equals 10021

def a = total_credit_summ.toInteger()

def b = summ_of_client_payments.toInteger()

client_need_to_pay =  a - b 

console give me back needed int value 2021:

2019-09-04 11:04:05.705 DEBUG testcase.Tarrif limit test case          - 9: println(client_need_to_pay)
2021
1 Like

It’s because you don’t use “as Integer” as I told you.

1 Like

@arturs.pumpa as Integer is the groovy way for the java’s .toInteger :wink:

1 Like