2
\$\begingroup\$

I started practicing on HackerRank to see how my learning is going on with Java. I wrote this code for problem which involved input having:

  1. String with maximum length of 10, followed by
  2. A 3 digit or lesser integer

Examples: "java 100", "Amazing 98"

Expected output:

  1. String should be left-justified with trailing white spaces till 15th character
  2. Leading digit of integer starts at 16th place with leading zeroes if it is less than 3 digits(padding) to make up for 18 characters

Examples: "java 100", "Amazing 098"

package HackerRank;

import java.util.ArrayList;
import java.util.Scanner;

public class IOFormatting {
    public ArrayList<String> getAllLines(){
        ArrayList<String> allInputs = new ArrayList<String>();
        Scanner getIp = new Scanner(System.in);
        while(getIp.hasNext()) {
            allInputs.add(getIp.nextLine());
        }
        getIp.close();
        return allInputs;
    }

    public ArrayList<Character> getTheInputAsArrayList(String input) {
        ArrayList<Character> inputLine = new ArrayList<Character>();
        for(int itr1=0;itr1<input.length();itr1++) {
            inputLine.add(input.charAt(itr1));
        }
        return inputLine;
    }

    public ArrayList<Character> formatTheArrayList(ArrayList<Character> inputLine){
        int itr2 = getBreakPoint(inputLine);
        for(int itr3=itr2+1;itr3<15;itr3++) {
            inputLine.add(itr3,' ');
        }
        return inputLine;
    }

    public int getBreakPoint(ArrayList<Character> inputLine){
        for(int itr2=0;itr2<=10;itr2++) {
            if(inputLine.get(itr2) == ' ') {
                return itr2;
            }
        }
        return 0;
    }

    public ArrayList<Character> doIntegerPadding(ArrayList<Character> inputLine){
        int padSize = 18 - inputLine.size();
        if(padSize>0) {
            for(int itr4=15;itr4<=14+padSize;itr4++) {
                inputLine.add(itr4,'0');
            }
        }
        return inputLine;
    }

    public String convertToFinalString(ArrayList<Character> inputLine) {
        String outputString = "";
        StringBuffer sb = new StringBuffer();
        for (Character s : inputLine) {
             sb.append(s);
        }
        outputString = sb.toString();
        return outputString;
    }

    public static void main(String args[]) {
        IOFormatting io = new IOFormatting();
        ArrayList<String> allInputs = io.getAllLines();
        System.out.println("================================");
        for(int mainItr=0;mainItr<allInputs.size();mainItr++) {
            String input = allInputs.get(mainItr);
            ArrayList<Character> inputLine = io.getTheInputAsArrayList(input);
            inputLine = io.formatTheArrayList(inputLine);
            inputLine = io.doIntegerPadding(inputLine);
            String outputString = io.convertToFinalString(inputLine);
            System.out.println(outputString);
        }
        System.out.println("================================");
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You are vastly overcomplicating the solution. The Scanner is perfectly capable of reading a string and an integer separately. Then, it is a simple matter of calling System.out.printf() with the appropriate width specifications:

  • %-15s for a string, left-justified, with trailing spaces
  • %03d for an integer, zero-padded

Also, Scanner is AutoCloseable, so you should use a try-with-resources block instead of calling .close() on it manually.

import java.util.Scanner;

public class IOFormatting {
    public static void main(String[] args) {
        try (Scanner input = new Scanner(System.in)) {
            while (input.hasNext()) {
                System.out.printf("%-15s%03d%n", input.next(), input.nextInt());
            }
        }
    }
}

If you really wanted to do the formatting the hard way by inserting spaces and zeroes, I suggest editing each line using StringBuilder.insert() — your ArrayList<Character> is a rather unnatural way to edit a string.

\$\endgroup\$
1
  • \$\begingroup\$ Great!!, I've got new stuff to read. Moreover, I wanted someone to tell me what you just did : "Unnatural" \$\endgroup\$ Commented Oct 31, 2018 at 3:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.