Simple java help..


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Simple java help..

  1. #1
    Join Date
    Dec 2004
    Posts
    1

    A java logic challenge..

    table1 --------------------------------------
    key(int) | question_no(int) | answer(String)
    --------------------------------------------
    1 | 1 | Hi
    2 | 2 | Nice
    3 | 2 | Cap
    4 | 3 | Good
    5 | 3 | Job
    6 | 4 | Wow!

    I need output this way:-

    ---------------------------------------------
    key(int) | question_no(int) | answer(String)
    --------------------------------------------
    1 | 1 | Hi
    2 | 2 | Nice Cap
    3 | 3 | Good Job
    4 | 4 | Wow!


    The above is concatenating answer(String) feild if Question_no and one after that are same...
    Can someone please help me with the logic. I cant get this to work...

    SList = dataTestCtrl.RetriveQuestions(); // function to retrieve the database select * from table1
    int size = SList.length;

    for (int i=0; i < size ; i++)
    {

    //THis is where i'm lost .. please help..
    }

    =============================================

    Thanks again !
    -lucky-

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I don't know the workings of 'dataTestCtrl.RetriveQuestions();' but I assume it returns a table record as an array of objects. So, having done that assumtion, I guess your loop should be something like this.
    Code:
    // make variables for the previous iteration
    String lagAnsw=null;
    int lagQNo=-1;
    
    for (int i=0; i < SList.size() ; i++) {
      Object [] record=(Object[])SList.get(i);
      int key=((Integer)record[0]).intValue();
      int qNo=((Integer)record[1]).intValue();
      String answ=(String)record[2];
      if (i>0) {
        if (lagQNo==qNo) {
          System.out.println(key+"|"+lagAnsw+"|"+answ);
        }
      }
      lagAnsw=answ;
      lagQNo=qNo;
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center