Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

how to write this logic

ArrayList al = new ArrayList();
      al.add(1+"");
      al.add(2+"");
      al.add(4+"");
     
     
     


if Id ends with -3 i want to add like this id-1,id-2 in to the list;
if Id ends with -5 id-1,id-4 in to the list
if Id ends with -6 id-2,id-4 in to the list;
if Id ends with -7 i have written logic like this;

it suffice my requirment but if the id ends with -3,-5,-6,-7 do i write to logic like this only.

                  while(rs.next())
                  {
                        String Id = rs.getString(2);
                        if(Id.endsWith("-7"))
                        {
                        
                          String res = Id.substring(0,Id.length()-2);
                          System.out.println(res);
                          for(int i=0;i<al.size();i++)
                          {
                            list.add(rs.getString(1)+"-"+res+"-"+al.get(i));
                          }
                        }
                        
                        }
Avatar of Ajay-Singh
Ajay-Singh

> if Id ends with -3 i want to add like this id-1,id-2 in to the list;
what is id-1 and id-2 here?
Avatar of chaitu chaitu

ASKER

> if Id ends with -3 i want to add like this id-1,id-2 in to the list;
what is id-1 and id-2 here?

i want to split Id-3 to Id-1 ,Id-2

id-1 denotes items;
id-2 denotes requirements;
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
show some examples, chaituu, we r not getting ur req.....

~marchent~

Id.substring(Id.length()-1) returns String ;
like this;this applies only when
Id.endsWith("-7") ;i want common logic for all;

  while(rs.next())
               {
                     String Id = rs.getString(2);
                    if(Id.endsWith("-7"))
                    {
                   
                      String res = Id.substring(0,Id.length()-2);
                      System.out.println(res);
                      for(int i=0;i<al.size();i++)
                      {
                        list.add(rs.getString(1)+"-"+res+"-"+al.get(i));
                      }
                    }
                   
                    }
> Id.substring(Id.length()-1) returns String ;

typo, shopuld have been

int id = Integer.parseInt(Id.substring(Id.length()-1));
excellent objects
chaituu, objects -

can you guys pls explain the question? I tried hard but i could not understand it