Need some logic help.
-
Hi people, i am doing some 'while' loop but kinda stuck somewhere. What i want to do is actually is comparing from 2 database tables. The problem now is example i have 10 records in SMS_IN table, but my code only runs the first record everytime i calls it. By right, it should loop thru all the data and check. SMS_IN (sample data) originator | text | date 88411254 | Internet bla bla bla | 5 August 78412447 | Quiz ans1 ans2 ans3 | 5 August USERAPP (sample data) userid | keyword 54 | Internet 80 | Quiz So the logic is first it will check the SMS_IN text column, example the first word of the first result is 'Internet', then it will check the USERAPP keyword column, if the keyword 'Internet' exists, means its a valid sms. If the keyword does not exists at all in the USERAPP table, the msg will be deleted from SMS_IN table this is my code
ResultSet rs = null; ResultSet rs2 = null; String sql = "Select \* from smsserver\_in"; String sql1 = "Select \* from USERAPP"; rs = db.SelectDB(sql); rs2 = db.SelectDB(sql1); try { String firstword = null; String keyword = null; int number = 0; while (rs.next()) { StringTokenizer st = new StringTokenizer(rs.getString("text")); firstword = st.nextToken(); //out.println("1"+firstword); number = rs.getInt("id"); while (rs2.next()) { keyword = rs2.getString("keyword"); //out.println("2"+keyword); if (firstword.equals(keyword)) { out.println(number); String sql3 = "Insert into app\_" + keyword + "(originator,text)values(" + rs.getString("originator") + ",'" + rs.getString("text") + "')"; db.InsertDB(sql3); String sql4 = "delete from smsserver\_in where id=" + number; db.UpdateDB(sql4); } else { } } } } catch (Exception ex) { } }
-
Hi people, i am doing some 'while' loop but kinda stuck somewhere. What i want to do is actually is comparing from 2 database tables. The problem now is example i have 10 records in SMS_IN table, but my code only runs the first record everytime i calls it. By right, it should loop thru all the data and check. SMS_IN (sample data) originator | text | date 88411254 | Internet bla bla bla | 5 August 78412447 | Quiz ans1 ans2 ans3 | 5 August USERAPP (sample data) userid | keyword 54 | Internet 80 | Quiz So the logic is first it will check the SMS_IN text column, example the first word of the first result is 'Internet', then it will check the USERAPP keyword column, if the keyword 'Internet' exists, means its a valid sms. If the keyword does not exists at all in the USERAPP table, the msg will be deleted from SMS_IN table this is my code
ResultSet rs = null; ResultSet rs2 = null; String sql = "Select \* from smsserver\_in"; String sql1 = "Select \* from USERAPP"; rs = db.SelectDB(sql); rs2 = db.SelectDB(sql1); try { String firstword = null; String keyword = null; int number = 0; while (rs.next()) { StringTokenizer st = new StringTokenizer(rs.getString("text")); firstword = st.nextToken(); //out.println("1"+firstword); number = rs.getInt("id"); while (rs2.next()) { keyword = rs2.getString("keyword"); //out.println("2"+keyword); if (firstword.equals(keyword)) { out.println(number); String sql3 = "Insert into app\_" + keyword + "(originator,text)values(" + rs.getString("originator") + ",'" + rs.getString("text") + "')"; db.InsertDB(sql3); String sql4 = "delete from smsserver\_in where id=" + number; db.UpdateDB(sql4); } else { } } } } catch (Exception ex) { } }
Hi, your code is wrong, it has two nested loops but acts as if there were only one loop. Here is a very simple snippet with the same problem, it should help you in understanding what goes wrong:
int i1=0;
int i2=0;for (; i1<10; i1++) {
for (; i2<10; i2++) {
Console.WriteLine("i1="+i1+" i2="+i2);
}
}Now imagine you executing this code by hand. If you still don't see it, run the code. Then use the debugger, set a breakpoint, and learn. If you discover this, chances are you won't make the same mistake ever again. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, your code is wrong, it has two nested loops but acts as if there were only one loop. Here is a very simple snippet with the same problem, it should help you in understanding what goes wrong:
int i1=0;
int i2=0;for (; i1<10; i1++) {
for (; i2<10; i2++) {
Console.WriteLine("i1="+i1+" i2="+i2);
}
}Now imagine you executing this code by hand. If you still don't see it, run the code. Then use the debugger, set a breakpoint, and learn. If you discover this, chances are you won't make the same mistake ever again. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Okay, anyway my loop is working already, but just some problems. At which part should i add the code to delete the message after checking thru all the keywords and it does not match? I tried adding a else statement inside the if statement of my inner loop but its not working
ResultSet rs = null; ResultSet rs2 = null; try { String firstword = null; String keyword = null; int number = 0; String sql = "Select \* from smsserver\_in"; rs = db.SelectDB(sql); while (rs.next()) { StringTokenizer st = new StringTokenizer(rs.getString("text")); firstword = st.nextToken(); //out.println("1"+firstword); number = rs.getInt("id"); String sql1 = "Select \* from USERAPP"; rs2 = db.SelectDB(sql1); while (rs2.next()) { keyword = rs2.getString("keyword"); //out.println("2"+keyword); if (firstword.equals(keyword)) { out.println(number); String sql3 = "Insert into app\_" + keyword + "(originator,text)values(" + rs.getString("originator") + ",'" + rs.getString("text") + "')"; db.InsertDB(sql3); String sql4 = "delete from smsserver\_in where id=" + number; db.UpdateDB(sql4); } } } } catch (Exception ex) { }
modified on Tuesday, August 4, 2009 11:37 PM
-
Okay, anyway my loop is working already, but just some problems. At which part should i add the code to delete the message after checking thru all the keywords and it does not match? I tried adding a else statement inside the if statement of my inner loop but its not working
ResultSet rs = null; ResultSet rs2 = null; try { String firstword = null; String keyword = null; int number = 0; String sql = "Select \* from smsserver\_in"; rs = db.SelectDB(sql); while (rs.next()) { StringTokenizer st = new StringTokenizer(rs.getString("text")); firstword = st.nextToken(); //out.println("1"+firstword); number = rs.getInt("id"); String sql1 = "Select \* from USERAPP"; rs2 = db.SelectDB(sql1); while (rs2.next()) { keyword = rs2.getString("keyword"); //out.println("2"+keyword); if (firstword.equals(keyword)) { out.println(number); String sql3 = "Insert into app\_" + keyword + "(originator,text)values(" + rs.getString("originator") + ",'" + rs.getString("text") + "')"; db.InsertDB(sql3); String sql4 = "delete from smsserver\_in where id=" + number; db.UpdateDB(sql4); } } } } catch (Exception ex) { }
modified on Tuesday, August 4, 2009 11:37 PM
As you want to delete a message when the keyword is not recognized, the logic is: - get keyword from message - compare with ALL valid keywords - after comparison loop: if not found, delete message hence you need a boolean "found" flag. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.