-1

I have a string as follows

String str = "AUTHOR01BOOK"

In this string I want to add this number 00001. How can I do that?

I tried concatenate it but the output I got is AUTHOR01BOOK1. My code is not appending zeros. How can I do that?

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58

4 Answers4

4

You can use the print format.

String str="AUTHOR01BOOK";
int num = 000001;

System.out.printf("%s%06d", str, num);

or use the String.format function to store it in a variable:

String myConcat = String.format("%s%05d", str, num);

EDIT: To answer raju's follow up question about doing this in a loop, Create a method that will return the formatted string:

static String myConcatWithLoop(String str, int iteration){

    return String.format("%s%05d", str, iteration);
}

then call this in your loop:

for (int i = 1; i <= 100; i++) {
        System.out.println(myConcatWithLoop(str, i));

}
TraxX
  • 382
  • 2
  • 13
  • Can you expand your answer to get the String as well? Instead of just displaying it. – TM00 Feb 07 '18 at 05:39
  • %s is for string and %d is for decimal.. the 06 is for padding and also you can search for printf. There are many documentations and samples. – TraxX Feb 07 '18 at 05:42
  • @TraxX he means he wants to save the result in a variable. In answer just prints it. – Ayush Gupta Feb 07 '18 at 05:42
  • 1
    You can use the String.format and store it in a variable – TraxX Feb 07 '18 at 05:44
  • Hi Trax, suppose I put this inside a for loop and I want to change 00001 to 00002 till 100 but my requirement is 00001 should always be 5 digit means when loop comes to 10 number should be 00010 and when it is 100 it should be 00100. How can I do this by following your answers. It would be helpful if you can explain me this by code –  Feb 07 '18 at 11:27
  • Hi Traxx can you please explain me your solution in detail? I am facing some problem in understanding this –  Feb 15 '18 at 06:51
  • Should I explain about the first problem about printing or the loop one? – TraxX Feb 15 '18 at 07:50
  • %s is for string and %06d is for decimal. '0' in '%06d' your zero padding while '6' is the number of additional padding which "num variable" is included. So when we use '%06d' and num is 1 then the value is 000001 which is 5 zeros and 1. – TraxX Feb 15 '18 at 08:38
0

if you store '000001' in int datatype it will treat as an octal. That is

int a=000001; System.out.println(a); Output: 1 It will treat it as OCTAL number

So you cannot store a number beginning with 0 in int as compiler will typecast it. Therefore for that you have to work with Strings only :)

0

Please try the below code. It not only displays but also changes the string. StringUtils help us to pad the left zeros. Number 4 in the leftPad method denotes the number of zeros. Its not a dynamic solution but it fulfills your need.

import org.apache.commons.lang.StringUtils;
public class Interge {

    public static void main(String[] args) {
        int i =00001;
        String s= i+"";
        String result = StringUtils.leftPad(s, 4, "0");
        String fnlReslt = "AUTHOR01BOOK"+result;
        System.out.println("The String : " + fnlReslt);

    }
}
John Joe
  • 12,412
  • 16
  • 70
  • 135
Jayanth
  • 746
  • 6
  • 17
0

Another approach is use StringBuilder

public class JavaApplication {

    public static void main(String[] args) {
        JavaApplication ex = new JavaApplication();
        String str = "AUTHOR01BOOK";
        System.out.println(ex.paddingZero(str));
    }

    public String paddingZero(String str) {
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        sb.append("00001");
        return sb.toString();
    }
}
John Joe
  • 12,412
  • 16
  • 70
  • 135