java-output-formating

আজকে আমরা Hacker Rank এর অন্যতম প্রবলেম সল্ভ করব, ২ টি উপায়ে সল্ভ টিপস থাকবে।

প্রশ্নঃ
Java's System.out.printf function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using printf.
To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.

Input Format

Every line of input will contain a String followed by an integer.
Each String will have a maximum of 10 alphabetic characters, and each integer will be in the inclusive range from 0 to 999

Output Format

In each line of output there should be two columns:
The first column contains the String and is left justified using exactly 15 characters.
The second column contains the integer, expressed in exactly 3 digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes.

Sample Input

java 100
cpp 65
python 50 

Sample Output

================================
java           100
cpp            065
python         050
================================


Explanation

Each String is left-justified with trailing whitespace through the first 15 characters. The leading digit of the integer is the 16th character, and each integer that was less than 3 character, and each integer that was less than 3 digits now has leading zeroes.

সারমর্মঃ

সহজ বাংলায় বলতে হলে, ইনপুটের ক্ষেত্রে প্রতিটি ইনপুট লাইনে একটি String থাকবে এবং তারপরে একটি integer পূর্ণসংখ্যা থাকবে। প্রতিটি String সর্বাধিক ১০টি বর্ণমালা(characters) থাকবে এবং প্রতিটি integer ০ থেকে ৯৯৯ পর্যন্ত পরিসীমা(range) থাকবে।

আউটপুটের ক্ষেত্রে প্রতিটি আউটপুট লাইনে দুটি করে কলাম থাকবে: প্রথম কলামে String থাকেবে এবং ঠিক ১৫টি অক্ষর ব্যবহার করে সমানভাবে লেখা থাকবে। দ্বিতীয় কলামে integer থাকবে, যা ঠিক ৩টি সংখ্যায় প্রকাশ করা হবে; যদি ইউজারের ইনপুটে তিনটির কম সংখ্যা থাকে, তাহলে আপনাকে অবশ্যই আপনার আউটপুটের integer সংখ্যাগুলিকে প্রথমে শূন্য (০) দিয়ে পূরণ করতে হবে।

বিঃদ্রঃ
প্রতিটি String এর আউটপুট ১০ টি অক্ষর (characters) দেখাবে, এবং এর পর ১৫ অক্ষরের একটি স্পেস হবে তারপর integer সংখ্যাগুলো দেখাবে।
integer সখ্যার ক্ষেত্রে সর্বোনিম্ন প্রস্থ(length) হবে ৩টি সংখ্যা এবং সর্বোচ্চ প্রস্থ(lehgth) হবে ৯৯৯।

শর্তঃ
এই প্রবলেমটি সমাধান করার জন্য আপনাকে জাভার System.out.printf ফাংশনটি ব্যবহার করতে হবে।


========== সমাধান ==========



প্রথম উপায়

Java Code:

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("================================");
    for(int i=0;i<3;i++){
        String s1=sc.next();
        int x=sc.nextInt();

        if (s1.length() > 10){
            s1 = s1.substring(0, 10);
        }

        System.out.printf("%-15s%03d \n", s1, x);
    }
    System.out.println("================================");
}


দ্বিতীয় উপায়
public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
        String tenChar = "";
        System.out.println("================================");
        for(int i=0;i<3;i++){
        String s1=sc.next();
        int x=sc.nextInt();

        String tempConvert = String.valueOf(x);

        // cut up to 10 char
        if (s1.length() >= 10){
            tenChar = s1.substring(0, 10);
        }else {
            tenChar = s1;
        }

        // cut number
        if (tempConvert.length() < 3){
            if (tempConvert.length() == 1){
                System.out.printf("%-15s00%d \n", tenChar, x);
            }
            if (tempConvert.length() == 2){
                System.out.printf("%-15s0%d \n", tenChar, x);
            }
        }else if (tempConvert.length() >= 3){

            System.out.printf("%-15s%d \n", tenChar, x);
        }

    }
        System.out.println("================================");
}


Get code from Github

© VividNote