简体   繁体   中英

Java: How to prevent my function from printing a trailing comma?

public static void intMethod(int myInt)
    {
        System.out.print("All the numbers lower than " + myInt + " and composed only with digits 1 and/or 3: ");
        
        for(int i=1; i<myInt; i++)
        {
            int num = i;
            while (num > 0) 
            {
                if (num % 10 != 1 && num % 10 != 3)
                    break;
                num /= 10;
            }
            if (num == 0) 
            {
                System.out.print(i);
                System.out.print(", ");
            }
        }
    }

I need to prevent my intMethod function from printing the last comma at the end of my output, and i'm having trouble doing so. I've tried the following, but had no luck. Any ideas are appreciated!

public static void intMethod(int myInt)
    {
        System.out.print("All the numbers lower than " + myInt + " and composed only with digits 1 and/or 3: ");
        
        for(int i=1; i<myInt; i++)
        {
            int num = i;
            while (num > 0) 
            {
                if (num % 10 != 1 && num % 10 != 3)
                    break;
                num /= 10;
            }
            if (num == 0) 
            {
                System.out.print(i);
                if(i<myInt)
                {
                    System.out.print(", ");
                }
            }
        }
    }

I suggest you use a StringJoiner and something like

System.out.print("All the numbers lower than "
        + myInt + " and composed only with digits 1 and/or 3: ");
StringJoiner sj = new StringJoiner(", ");
for (int i = 1; i < myInt; i++) {
    int num = i;
    while (num > 0) {
        if (num % 10 != 1 && num % 10 != 3) {
            break;
        }
        num /= 10;
    }
    if (num == 0) {
        sj.add(String.valueOf(i));
    }
}
System.out.println(sj);

If on the current iteration you don't know whether you need to print comma or not then you can try to collect whole string first in a variable, remove last comma and print it.

StringBuilder sb = new StringBuidler();
for ... {
    sb.append(...).append(",");
}
if (sb.length() > 0) {  // if sb is not empty
    sb.deleteCharAt(sb.lastIndexOf(",")); // delete last comma
}
System.out.println(sb.toString());

You may use a StringBuilder with logic which only prepends a comma from the second matching number onwards:

public static void intMethod(int myInt) {
    System.out.print("All the numbers lower than " + myInt + " and composed only with digits 1 and/or 3: ");
    
    StringBuilder sb = new StringBuilder();

    for (int i=1; i<myInt; i++) {
        int num = i;
        while (num > 0) { 
            if (num % 10 != 1 && num % 10 != 3)
                break;
            num /= 10;
        }
        if (num == 0) {
            if (sb.length() > 0) sb.append(",");
            sb.append(i);
        }
    }

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

intMethod(50);

This prints:

All the numbers lower than 50 and composed only with digits 1 and/or 3: 1,3,11,13,31,33

You can fix your existing code by outputting the comma before the number (except for the first time):

if (num == 0) 
{
    if (i>1)
        System.out.print(", ");
    System.out.print(i);
}

Full code:

public static void intMethod(int myInt)
{
    System.out.print("All the numbers lower than " + myInt + " and composed only with digits 1 and/or 3: ");
    
    for(int i=1; i<myInt; i++)
    {
        int num = i;
        while (num > 0) 
        {
            if (num % 10 != 1 && num % 10 != 3)
                break;
            num /= 10;
        }
        if (num == 0) 
        {
            if (i>1)
                System.out.print(", ");
            System.out.print(i);
        }
    }
}

Output for intMethod(25); :

All the numbers lower than 25 and composed only with digits 1 and/or 3: 1, 3, 11, 13

Using a utility method as suggested by Elliott is usually the best way to go, but if you really want/need to do it manually using a boolean is an easy way to do it.

Based upon your first example, I've added a boolean to control the printing of the comma:

public static void intMethod(int myInt) {
    boolean printComma = false;   // Don't print the comma the first time around.

    System.out.print("All the numbers lower than " + myInt + " and composed only with digits 1 and/or 3: ");
        
    for(int i=1; i<myInt; i++)
    {
        int num = i;
        while (num > 0) 
        {
            if (num % 10 != 1 && num % 10 != 3)
                break;
            num /= 10;
        }
        if (num == 0) 
        {
            if (printComma) {
                // *After* the first iteration, the printComma will be true
                // so *before* we print the second and subsequent digits
                // we will output the comma that separates this digit from the previous one.
                System.out.print(", ");
            } else {
                // first time through, printComma is false, so we end up here
                // set our flag so that from now on, we will print the comma.
                printComma = true;
            }
            System.out.print(i);
            
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM