Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**
     * Formats by adding a hyphen for every 4 numbers (IE like a credit card)
     * @param s Charsequence being altered.
     * @return Return an altered String with hyphens in it
     */
    public static String formatNumbersAsCreditCard(CharSequence s) {
        int groupDigits = 0;
        String tmp = "";
        for (int i = 0; i < s.length(); ++i) {
            tmp += s.charAt(i);
            ++groupDigits;
            if (groupDigits == 4) {
                if (groupDigits == 16) {
                } else {
                    tmp += "-";
                }
                groupDigits = 0;
            }
        }
        if (tmp.length() == 20) {
            tmp = tmp.substring(0, tmp.length() - 1); //Get rid of last digit
        }
        return tmp;
    }
}