-2

Basically I've built up a string and I need to put an if statement on when to use a comma and a space. So basically I need it be after the first element and not on the last element.

This is what my current code is:

And the output it returns is

"thing1thing2thing3"

I want to make the output to be

"thing1, thing2, thing3"

And I need an if statement as part of the requirements on when to place the commas and spaces.

Thanks in advance.

John
  • 63
  • 7
  • What have you tried? If you know that you'd need a comma followed by a space, couldn't you simply concatenate that to your existing string (between the first and the second)? With regards to the actual if-statement, assuming you already have your second string, you could perform a check to see if the comma / space should be added (perhaps if the second string was not empty)? I'm assuming this is some type of homework or exercise, if you are wondering why I'm not just throwing an answer out there. – Rion Williams May 09 '17 at 02:32
  • I need it when it for more than one element and not to do the comma and space after the last element. – John May 09 '17 at 02:33
  • @John you can put a condition s != null or empty and put (comma + space) before each elemetn – Amit Phaltankar May 09 '17 at 02:34
  • Edited my initial post to make it clearer. – John May 09 '17 at 02:45

3 Answers3

0

This might be a little advanced for you, but it's very easy when using Java 8 (if things is a Collection:

return Optional.of(things.stream()
                         .filter(thing -> thing.getCategory() == things.STUFF)
                         .collect(Collectors.joining(", ")))
               .orElse("nothing");

If you're using Java 7, then you can do it manually:

StringBuilder sb = new StringBuilder();

for (Thing thing : things) {
    if (things.getCategory() == some.STUFF){
        sb.append(thing.getName()).append(", ");
    }
}

if (s.isEmpty()) {
    return "nothing";
}

return sb.delete(sb.length() - 2, sb.length()).toString();
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
0

I'd use a for-loop rather than a for-each loop - only because I see it as eliminating the need for an additional counter variable. This is how I'd approach the problem.

    String[] things = {"thing 1", "thing 2", "thing 3", "thing 4"};

    for(int i = 0; i < things.length; i++)
    {
        if(i < things.length - 1)
        {
            System.out.print(things[i] + ", ");
        }
        else
        {
            System.out.print(things[i] + ".");
        }
    }
Mick
  • 27
  • 7
-1

There are a few unclear things about the question, so the code below is based on what I have understood so far.

String s = "";
boolean isComma = true; // true = comma, false = space.
for (Thing thing : things)
{
    if (things.getCategory() == thing.STUFF)
    {
        //Check if there already exists an entry within the String.
        if (s.length() > 0)
        {
            //Add comma or space as required based on the isComma boolean.
            if (isComma)
            {
                s += ", ";
            }
            else
            {
                s += " ";
            }
        }
        s += thing.getName();
    }
}

if (s.equals(""))
{
    s += "nothing";
}
return s;
dat3450
  • 954
  • 3
  • 13
  • 27
  • So I've tried your method and now it gives me " thing1,thing2,thing3" so what do I need to add to make it look like "thing1, thing2, thing3"? – John May 09 '17 at 03:20
  • Change line where there is `s += ",";` to `s += ", ";` – dat3450 May 09 '17 at 03:22