I am writing a toString method where a it returns three integers. If any of them are less than ten I have to add a zero before the number. For example if I had the variables 5, 6, and 2 it would read 05:06:02. I really can't figure out how to implement this and I any help would be greatly appreciated.

Here is my current method

public String toString() {
        return "The current time is " + hour + ":" + minute + ":" + second;
    }
Member Avatar for coil

One way of doing it:

To test if a number is less than 10:

int number;
...
if(number<10) {
//do stuff
}

To add a zero, you must convert the int to a String with String s=Integer.toString(number) . Then you can simply concatenate a zero to the front, with s="0"+s; .

Thank you so much for the help. After a little testing I got it to work. Its a bit crude but here is my new code.

public String toString() {
        int hNumber;
        int sNumber;
        int mNumber;
        String m = new String();
        String h = new String();
        String s = new String();
        mNumber = minute;
        sNumber = second;
        hNumber = hour;
        
        if(mNumber < 10) {
            m = Integer.toString(mNumber);
            m = "0" + m;
        }
        if(mNumber >= 10) {
           m = Integer.toString(mNumber);
        }
        if(sNumber < 10) {
           s = Integer.toString(sNumber);
            s = "0" + s;
        }
        if(sNumber >= 10) {
            s = Integer.toString(sNumber);
        }
        if(hNumber < 10) {
            h = Integer.toString(hNumber);
            h = "0" + h;
        }
        if(hNumber >= 10) {
            h = Integer.toString(hNumber);
        }
        return "The current time is " + h + ":" + m + ":" + s;
    }

Thanx again.

Sorry to barge in after it's been solved, but with the number being an integer to begin with, you could have written:

if (number<10)
   number*=10;

Also, nest your if/else; they way you wrote it: it will test all your conditions. If Number isn't < 10 it most certainly is >= than 10, so no need to test in that second if.

Sorry to barge in after it's been solved, but with the number being an integer to begin with, you could have written:

if (number<10)
   number*=10;

Also, nest your if/else; they way you wrote it: it will test all your conditions. If Number isn't < 10 it most certainly is >= than 10, so no need to test in that second if.

No that is completely wrong and you should read first the previous posts more carefully.
The idea is to convert the integer: 5 to: "05" With your way the number 5, would become 50!

Also a more simple way would be:

if(mNumber < 10) {
  m = "0" + mNumber;
}

When you concatenate numbers and Strings the conversion to String happens automatically. If you simply want to convert an int to a String then the above is not the best approach and use the method you are already using. But if you want to concatenate an int with a String then the above is simpler.

Sorry, then. I didn't read through :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.