Avatar of chaitu chaitu
chaitu chaitu
Flag 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));
                          }
                        }
                        
                        }
Java

Avatar of undefined
Last Comment
Ajay-Singh

8/22/2022 - Mon
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?
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
Mick Barry

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
marchent

show some examples, chaituu, we r not getting ur req.....

~marchent~
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
chaitu chaitu

ASKER

Id.substring(Id.length()-1) returns String ;
chaitu chaitu

ASKER
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));
                      }
                    }
                   
                    }
Mick Barry

> Id.substring(Id.length()-1) returns String ;

typo, shopuld have been

int id = Integer.parseInt(Id.substring(Id.length()-1));
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
chaitu chaitu

ASKER
excellent objects
Ajay-Singh

chaituu, objects -

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