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?

Aleksandr Podkutin
  • 2,532
  • 1
  • 20
  • 31
rocking
  • 4,729
  • 9
  • 30
  • 45
  • 2
    Start by using a `StringBuilder` – MadProgrammer Jun 12 '14 at 07:32
  • Add a condition to check if current iteration is last, and add "And" only if conditions is false. – shree.pat18 Jun 12 '14 at 07: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: http://stackoverflow.com/questions/187676/java-equivalents-of-c-sharp-string-format-and-string-join – weston Jun 12 '14 at 07:40

8 Answers8

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);
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
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
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 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 Jun 12 '14 at 07:40
  • 1
    @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 – MadProgrammer Jun 12 '14 at 07:42
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);
    }
}
Nirav Prajapati
  • 2,987
  • 27
  • 32
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());
Kasper Ziemianek
  • 1,329
  • 8
  • 15
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 ";
} 
sujithvm
  • 2,351
  • 3
  • 15
  • 16
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.

PKlumpp
  • 4,913
  • 8
  • 36
  • 64
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.

jwenting
  • 5,505
  • 2
  • 25
  • 30
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);
  }
premji
  • 11
  • 5