Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static final String SPACE_SEPERATOR = " ";

    public static String handleCardNumber(String inputCardNumber) {

        return handleCardNumber(inputCardNumber, SPACE_SEPERATOR);
    }

    public static String handleCardNumber(String inputCardNumber, String seperator) {

        String formattingText = inputCardNumber.replace(seperator, "");
        String text;

        if (formattingText.length() >= 4) {

            text = formattingText.substring(0, 4);

            if (formattingText.length() >= 8) {
                text += seperator + formattingText.substring(4, 8);
            } else if (formattingText.length() > 4) {
                text += seperator + formattingText.substring(4);
            }

            if (formattingText.length() >= 12) {
                text += seperator + formattingText.substring(8, 12);
            } else if (formattingText.length() > 8) {
                text += seperator + formattingText.substring(8);
            }

            if (formattingText.length() >= 16) {
                text += seperator + formattingText.substring(12);
            } else if (formattingText.length() > 12) {
                text += seperator + formattingText.substring(12);
            }

            return text;

        } else {
            text = formattingText.trim();
        }

        return text;
    }
}