How to use output from a splited sms [closed]

2019-09-12 01:24发布

问题:

How can l capture the output from a split and use it for example as an action or comparing with data in the database

String myString =  (msg.getText());

String[] a = myString.split("\\*");

for (String b : a)
    System.out.println(b);

the output is:

pay 
merchant
amount

want to use pay as an action and amount to increase the amount of the merchant in the database

回答1:

You will need to define the rules for how to translate the input into an action on the database. To get you started, you can write if statements like this for each rule:

if (a.length == 3 && a[0] == "pay" && a[1] == "merchant")
{
    double amount = Double.parseDouble(a[2]);
    // connect to database and add amount to the merchant
    // UPDATE [AmountTable] SET [Amount] = [Amount] + ?
    // WHERE [Type] = 'Merchant' (or whatever the logic is)
}


回答2:

String [] message_parts = message.split("#");
// check format
String pay = message_parts[0];
String merchant = message_parts[1];
String amount_string = message_parts[2];