需要一些逻辑帮助. [英] Need some logic help.
问题描述
我正在做一些"while"循环,但是有点卡在某个地方.
我要做的实际上是从2个数据库表中进行比较.
现在的问题例如,我在SMS_IN表中有10条记录,但是我的代码每次调用它时都只运行第一条记录.正确,它应该遍历所有数据并进行检查.
SMS_IN(样本数据)
originator |文字|日期
88411254 |互联网bla bla bla | 8月5日
78412447 |测验ans1 ans2 ans3 | 8月5日
USERAPP(示例数据)
userid |关键字
54 |互联网
80 |测验
所以逻辑是首先将检查SMS_IN文本列,例如第一个结果的第一个单词是"Internet",然后将检查USERAPP关键字列,如果关键字是"Internet" ''存在,表示其有效的短信.如果USERAPP表中根本不存在该关键字,则将从SMS_IN表中删除该消息.
这是我的代码
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;<br /> ResultSet rs2 = null;<br /> String sql = "Select * from smsserver_in";<br /> String sql1 = "Select * from USERAPP";<br /> rs = db.SelectDB(sql);<br /> rs2 = db.SelectDB(sql1);<br /><br /> try {<br /> String firstword = null;<br /> String keyword = null;<br /> int number = 0;<br /> while (rs.next()) {<br /> StringTokenizer st = new StringTokenizer(rs.getString("text"));<br /> firstword = st.nextToken();<br /> //out.println("1"+firstword);<br /> number = rs.getInt("id");<br /><br /> while (rs2.next()) {<br /> keyword = rs2.getString("keyword");<br /> //out.println("2"+keyword);<br /> if (firstword.equals(keyword)) {<br /> out.println(number);<br /> String sql3 = "Insert into app_" + keyword + "(originator,text)values(" + rs.getString("originator") + ",''" + rs.getString("text") + "'')";<br /> db.InsertDB(sql3);<br /> String sql4 = "delete from smsserver_in where id=" + number;<br /> db.UpdateDB(sql4);<br /> } else {<br /><br /> }<br /><br /> }<br /><br /> }<br /> } catch (Exception ex) {<br /> }<br /> }
推荐答案
您的代码是错误的,它有两个嵌套循环,但就像只有一个循环一样.
这是一个非常简单的代码段,存在相同的问题,它应该可以帮助您理解问题所在:
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:
<br />int i1=0;<br />int i2=0;<br /><br />for (; i1<10; i1++) {<br /> for (; i2<10; i2++) {<br /> Console.WriteLine("i1="+i1+" i2="+i2);<br /> }<br />}<br />
现在想象您手动执行此代码.如果仍然看不到,请运行该代码.然后使用调试器,设置断点,然后学习.
如果您发现了这一点,很可能就不会再犯同样的错误了.
:)
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.
:)
这篇关于需要一些逻辑帮助.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!