2

Hi my requirement is to produce following string:

qid<> 0 And qid<> 1 And qid<> 2 And qid<> 3 And qid<> 4 And qid<> 5 And qid<> 6 And qid<> 7 And qid<> 8 And qid<> 9 

So I did like this:

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
       String deleteQids="qid<> ";
       String deleteqidString1="";

       for(int i=0;i<10;i++) {
         deleteqidString1+=deleteQids+i +" And ";
       }
       System.out.println(deleteqidString1);
    }
}

http://ideone.com/dpFfxF

But at the end And appears.

Can anybody please tell me how to get rid of this?

3
  • 2
    Start by using a StringBuilder Commented Jun 12, 2014 at 7:32
  • Add a condition to check if current iteration is last, and add "And" only if conditions is false. Commented Jun 12, 2014 at 7:35
  • Note that C# has a handy string.Join method that you can add " and " inbetween each element of a list and this answer gives ideas for doing the same in java: stackoverflow.com/questions/187676/… Commented Jun 12, 2014 at 7:40

8 Answers 8

3

You could just handle the first occurrence explicitly:

String deleteqidString1 = deleteQids+i;
for(int i=1; i < 10; i++) {
    deleteqidString1 += " And " + deleteQids + i;
}

EDIT:
As noted in the comments, a StringBuilder is the right tool for dynamically building tools. (This doesn't effect the logic of the solution, though):

StringBuilder deleteqidString1 = new StringBuilder(deleteQids).append(i);
for(int i=1; i < 10; i++) {
    deleteqidString1.append(" And ").append(deleteQids).append(i);
}
Sign up to request clarification or add additional context in comments.

3 Comments

My only criticism of this approach is it assumes that there is at least one "qid"
@MadProgrammer yup, agreed. The OP seems to assume this limitation, so I went along. The solution could easily be improved to check handle the case of 0 "qid"s.
Yep, only a criticism, not a reason for a downvote ;) +1
2

You could simply use String#substring to remove the last " And " from the String, but a more efficent method would to use a StringBuilder and "prefix" " And " to it when the index is greater than 0, for example

String deleteQids = "qid<> ";
StringBuilder sb = new StringBuilder(128);
for (int i = 0; i < 10; i++) {
    if (i > 0) {
        sb.append(" And ");
    }
    sb.append(deleteQids).append(i);

}
System.out.println(sb.toString());

Which ends up printing something like...

qid<> 0 And qid<> 1 And qid<> 2 And qid<> 3 And qid<> 4 And qid<> 5 And qid<> 6 And qid<> 7 And qid<> 8 And qid<> 9

2 Comments

What is the reason to apply an initial capacity to the StringBuilder? Is it faster if you know that the specified size will be used anyway?
@brimborium Yes, like ArrayList, StringBuilder uses a char array internally, so when it reaches it's limit, the array needs to be resized, initialising the initial capacity reduces the potential overhead of this operation
1

If you operates much on strings always use StringBuilder because it's more efficient.

  int qids = -1;
  StringBuilder sb = new StringBuilder();

  if(qids>=0) {
    for(int i=0;i<qids-1;i++)
    {
      sb.append("qid<> ");
      sb.append(i);
      sb.append(" And ");

    }

    sb.append("qid<> ");
    sb.append(qids);
  }

  System.out.println(sb.toString());

2 Comments

FYI- This assumes that there is at least one qid
You were right, but now it doesn't
1

if your iteration is fixed upto 10 then

than try this one it may help this is not optimum way to do this but still you can get what you want

if iteration is fixed to 10

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
                String deleteQids="qid<> ";String deleteqidString1="";
for(int i=0;i<10;i++)
{
            if(i==9)
            {
                 deleteqidString1+=deleteQids+i ;
            }
            else
            {
                 deleteqidString1+=deleteQids+i +" And ";
            }


}
System.out.println(deleteqidString1);
    }
}

Comments

1

The code snippet given below does the job. It adds 'And' for every iteration except for the last one ie. when i equals 9.

String deleteQids="qid<> ";
String deleteqidString1="";
for(int i=0;i<10;i++)
{
    deleteqidString1 += deleteQids + i ;
    if (i < 9) deleteqidString1 += " And ";
} 

2 Comments

Please point the OP what you have done, and explain what your code is doing.
Edited the answer. Hope it helps.
0

You better use a StringBuilder for this. And then, you have to find a system to know where to put your "And".

Here is some hint:

  • An "And" comes after an Integer. (But not after the last one)
  • An "And" is followed by a "qid<>"

Combine these conditions, and you can build your String.

Comments

0

Either change your logic to not append that "And" if you're in the last iteration of the loop, or after you are done looping modify the String to remove it.
Those are basically your 2 options, which you'd choose would depend on personal preference and the exact application logic.
In this simple case I'd elect the first, in a more realistic scenario where the String is constructed using a StringBuilder and an Iterator over some Collection the second option is often easier to implement and certainly easier to read.

So something along the lines of

deletequidString1 += deleteQuids+1;
if (i < 10) deleteQuidString1 += " And ";

would be your best bet here.

Comments

0

Use this code

   String deleteQids="qid<> ";
   String deleteqidString1="";

   for(int i=0;i<10;i++) {
      if(i!=9)
  {   
     deleteqidString1+=deleteQids+i +" And ";
 }
 else
 {
     deleteqidString1+=deleteQids+i +"";
 }
   }
   System.out.println(deleteqidString1);
  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.