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?

2
  • For the number say 12345 how do you want it to appear? For the number 5 how do you want it to appear? Commented Jul 30, 2015 at 1: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' Commented Jul 30, 2015 at 1:55

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

6 Comments

I wonder where you got this code from: stackoverflow.com/questions/275711/…
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.
This will work given the confines of the OP that the number num will always be less than 1000.
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 - See my edit for example, and post your code if you're getting errors.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.