Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Need some logic help.

Need some logic help.

Scheduled Pinned Locked Moved Algorithms
databasehelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    benjamin yap
    wrote on last edited by
    #1

    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) {
        }
    }
    
    L 1 Reply Last reply
    0
    • B benjamin yap

      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) {
          }
      }
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      B 1 Reply Last reply
      0
      • L Luc Pattyn

        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.


        B Offline
        B Offline
        benjamin yap
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • B benjamin yap

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups