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 forward slash every 2 numbers (IE like a credit card expiration date)
     * @param s Charsequence being altered.
     * @return Return an altered String with hyphens in it
     */
    public static String formatNumbersAsCreditCardExpiration(CharSequence s) {
        int groupDigits = 0;
        String tmp = "";
        for (int i = 0; i < s.length(); ++i) {
            tmp += s.charAt(i);
            ++groupDigits;
            if (groupDigits == 2) {
                tmp += "/";
                groupDigits = 0;
            }
        }
        if (tmp.length() > 5) {
            tmp = tmp.substring(0, tmp.length() - 1); //Get rid of last digit
        }
        return tmp;
    }
}