3

I am trying to add zeros to a number input by a user by using the printf() function. However, I am unsure of the syntax usage. Here is what I have so far:

public class Solution {

    public static void main(String[] args) {
            Scanner reader = new Scanner(System.in);
            final int requiredNumLength = 3;
            // readign untill EOF
            while (reader.hasNext()){
                    // getting the word form the input as a string
                    String word = reader.next();
                    // getting the number from the input as an integer
                    int num = reader.nextInt();
                    int length = String.valueOf(num).length();
                            if (length < requiredNumLength){
                                    int zerosRequired = requiredNumLength - length;
                                    //System.out.println("required zeros: " + zerosRequired);
                            }
                    // print out the columns using the word and num variables
                    System.out.printf("%-10s  %-10s%n" , word, num);

            }
    }

}

and here is an example of it being used:

input : java 023
output: java          023

(That works fine)

Now my problem is, in a case where a number is less than 3 characters I want to be able to append zeros in front to make it be a length of 3. Furthermore, that is what I am hopping to do with the if statement that finds how many zeros a number needs. I was thinking of using something like this to the printf() function: ("%0zerosRequiredd", num); however, I do not know how to use it in conjunction with what I already have: System.out.printf("%-10s %-10s%n" , word, num). Any suggestions?

Bond
  • 16,071
  • 6
  • 30
  • 53
Alfredo
  • 115
  • 2
  • 9
  • For the number say `12345` how do you want it to appear? For the number `5` how do you want it to appear? – Tim Biegeleisen Jul 30 '15 at 01:52
  • @TimBiegeleisen sorry, I think I forgot to mention that the user only puts numbers <= 3 digits long. Therefore i don't need to worry about anything more than 3. For the number 5 for example, I want it to appear as '005' – Alfredo Jul 30 '15 at 01:55

1 Answers1

5

You can do something like this:

String formatted = String.format("%03d", num);

That will lead with however many zeros.

For your example, you can use:

System.out.printf("%-10s %03d" , word, Integer.parseInt(num));

If num is a float, use Float.parseFloat(num), or better yet declare it as the correct type.

See below:

 public static void main(String[] args){
    String word = "Word";

    int num = 5;
    System.out.printf("%-10s  %03d\n" , word, num);

    num = 55;
    System.out.printf("%-10s  %03d\n" , word, num);

    num = 555;
    System.out.printf("%-10s  %03d\n" , word, num);

    num = 5555;
    System.out.printf("%-10s  %03d\n" , word, num);

    num = 55555;
    System.out.printf("%-10s  %03d\n" , word, num);
    }

Here is the output:

mike@switters:~/code/scratch$ javac Pf.java && java Pf
Word        005
Word        055
Word        555
Word        5555
Word        55555
mikeb
  • 10,578
  • 7
  • 62
  • 120
  • I wonder where you got this code from: http://stackoverflow.com/questions/275711/add-leading-zeroes-to-number-in-java – Tim Biegeleisen Jul 30 '15 at 01:51
  • how do I add that to : System.out.printf("%-10s %-10s%n" , word, num)? i am unsure of where to add it. Or does it have to be a separate line? Also, if I do it that way, how does run time know how many zeros to add? I assume that "%03d" will add 3 zeros. However, it maybe the case that only 1 or 2 zeros are needed to make the number 3 digits long. – Alfredo Jul 30 '15 at 01:54
  • This will work given the confines of the OP that the number `num` will always be less than 1000. – Tim Biegeleisen Jul 30 '15 at 02:17
  • I am getting a compile time error when using System.out.printf("%-10s %03d" , word, Integer.parseInt(num)); . I get error: no suitable method found for passing(int) - its for that same line. Any ideas ? – Alfredo Jul 30 '15 at 02:36
  • @Alfredo - See my edit for example, and post your code if you're getting errors. – mikeb Jul 30 '15 at 11:21
  • @TimBiegeleisen - Not quite. It will work just fine with any number. Go back and re-read the question: `in a case where a number is less than 3 characters I want to be able to append zeros in front to make it be a length of 3`. – mikeb Jul 30 '15 at 11:23