简体   繁体   中英

Outputting a leading zero an int variable

I'm trying to output a series binary digits and next series of binary digits depend on the previous series's of binary digits. Some of these series lead with one or more zeroes. I can only use int variables.

So if the program starts with 0 , the next output will be 01 and then 0110 and so on. Will I have to hard code a leading a zero if the int starts with a zero or is there another way I can do this?

Also, is Integer.toBinaryString() the only way to output a number to binary?

Just format the output, you syntax is meaningless because the value of 01 is 1 , the compiler just store the value as a number, not as a sequence of digits. Try with:

int x = 1;
System.out.printf("%02d", x);

To be more precise when you place a leading 0 the number literal is interpreted as octal (base 8). So for 01 it's ok but 08 would be illegal (since 8 is not a valid octal digit). This is true for many programming languages.

In any case don't confuse a number literal with a numeric value . The compiler turns a literal into a value and just the value is what is effectively used at runtime.

If your problem is really just this simple, then you should try this:

System.out.print("0"+x);

As you may be aware of, integer stores values. And what gets output to the screen is converted to be Strings. An integer is going to be converted to a tring without the leading zeroes.

By the way, if you start a numeric constant with a 0, in Java that means that you are specifying an octal number.

Recommended reading: Java literals

If however your problem is deeper, like you should output numbers in given formats, you should definitely read up on DecimalFormat

System.out.println(new DecimalFormat("00").format(x)); 

The type int just stores an numeric integer value and nothing more. An int variable does not store the leading zeros, nor the base in which the number was entered. In your case the leading zero means an octal literal, which is probably not what you meant anyway.

If you want to store the leading zeros you have to do using a different approach, for example using a string as you suggest.

As far as i know, no because java trys to save ram usage by removing un-needed 0's. The best way would be to create it into a string and create a method that can times, add, take away, devide a string integer by a string integer(times no's and then readd the 0's to the start).

EDIT

public String addStringInteger(String x1, Integer i){
    String leading0 = "";
    String nol = "";
    boolean onLeading = true;
    for(char c : x1.toCharArray()){
        String l = Character.toString(c);
        if(onLeading && l.equals("0")){
            leading0 += "0";
        }else{
            onLeading = false;
            nol += l;
        }
    }
    int intO = Integer.parseInt(nol) + i; //you can change + to x, -, / etc
    return leading0 + ""+ intO;
}

this example would allow you to do addStringInteger("001", 20) and you would get 0021 back, the method could be edited so you get 021 back, but i made that as a quick example, this sort of method should be able to perform anything.

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