0

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

mellamokb
  • 56,094
  • 12
  • 110
  • 136
  • 1
    The question itself does not reflect on the question title, you want to consider changing the question title or close this question as previous commenter has given to you link that you should read. – Jasonw Apr 26 '12 at 01:45
  • so for example l want to take the amount variable and compare with the one in my database. That is deduct the personal account amount and add to the merchant account. how can l do that. plz help – user1263516 Apr 26 '12 at 08:08
  • 1
    Do you have rules defined for what valid output is allowed, and how those translate into actions on the database? – mellamokb Apr 27 '12 at 13:56
  • dont know how to implement the rules – user1263516 Apr 27 '12 at 14:06
  • 1
    [Stack Overflow is not your personal research assistant](http://meta.stackexchange.com/a/128553/172496). What may be confusing you is that [Stack Overflow is not like all those other sites](http://meta.stackexchange.com/a/128554/172496) – Brian Roach Apr 27 '12 at 15:40
  • @BrianRoach You like it don't you? ;-) – assylias Apr 27 '12 at 15:41
  • @assylias - I got tired of typing (effectively) the same thing over and over. Now I just copy and paste that. – Brian Roach Apr 27 '12 at 15:42

2 Answers2

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)
}
mellamokb
  • 56,094
  • 12
  • 110
  • 136
  • its saying double cannot be deferenced, " double amount = double.parseDouble(a[2]); " – user1263516 Apr 27 '12 at 14:10
  • Ah. Sorry, guess i'm too familiar with C# :) Should be fixed now. – mellamokb Apr 27 '12 at 14:40
  • okay so the method is now passing the amount to double. actually the text will come in the form...pay*merchant*2000.00, – user1263516 Apr 27 '12 at 14:43
  • OK, you know the `split` method; I've given you an example of how to process the input result; are you missing knowledge of something in Java to implement your business requirements? We're not going to do your coding for you. If you have all the pieces you need, it's up to you to pull them together and implement the solution. – mellamokb Apr 27 '12 at 14:54
  • okay thanks, lm a student doing a project. lm new to java,have been doing c thats wy lm keep on asking – user1263516 Apr 27 '12 at 14:59
  • The process of breaking down your high-level process into step-by-step instructions for a computer program is not specific to any programming language. Is there any syntax or functionality you don't know in Java that you need yet? The syntax itself is like 90% similar between Java and C. – mellamokb Apr 27 '12 at 15:01
0
String [] message_parts = message.split("#");
// check format
String pay = message_parts[0];
String merchant = message_parts[1];
String amount_string = message_parts[2];
necromancer
  • 23,916
  • 22
  • 68
  • 115
  • so for example l want to take the amount variable and compare with the one in my database. That is deduct the personal account amount and add to the merchant account. how can l do that. plz help – user1263516 Apr 26 '12 at 08:06
  • The easiest thing would be to use JDBC to connect to the database so you can run SQL queries. The SQL query to deduct from personal account amount would look something like: `update personal_account_table set balance = balance - amount where person_id = "p0003"` and for merchant: `update merchant_account_table set balance = balance + amount where merchant_id = "m0007"` (assuming the tables exist and have varchar id fields). If you want to do extra (no need if you don't understand), then include the 2 SQL statements in a single JDBC transaction so that they are done atomically. – necromancer Apr 26 '12 at 19:29
  • You might need another SQL statement to check if the person's `balance >= amount` (and if you are doing transactions, also include it in the same transaction). – necromancer Apr 26 '12 at 19:31
  • so using this query is it going to take the variable amount from the splited message – user1263516 Apr 27 '12 at 00:55
  • no. do each part separately first and i can help you integrate them. the first part is to extract the amounts which is in the answer. the second part is to update the database -- you should ask a separate question for updating the database, but i have already given you most of the answer to the second part. now, you must write some code that works and i can help you improve it. – necromancer Apr 27 '12 at 05:36
  • l have an MySQL databae with two tables personal account and merchant account. the personal account have attributes (ID BALANCE NAME SHORTNAME) and merchant account with (ID ACCOUNTNUMBER BALANCE MOBILENUMBER NAME SURNAME). l wrote this query but lm getting an error //ResultSet rs23 = stmt.executeQuery("update personal_account_table set BALANCE = BALANCE - amount where person_id = '"100"'"); – user1263516 Apr 27 '12 at 14:29
  • use the correct table name and column names. and change the part `person_id = "100"` to `mobilenumber = "123456789"`. – necromancer Apr 27 '12 at 18:06