Java program: Get character before index

Java program: Get character before index

Write a Java program to get the character (Unicode code point) before the specified index within the string.


public class demo {
    public static void main(String[] args) {

        String str = "GulshanKumar";
        int givenIndex = 4;
        int len = str.length();

//        Index should be more than 0 and less than the length-1 of string because
//        0 index is the last index in string
        if ((givenIndex>0) && (givenIndex<len)){
            for (int i=0; i<len; i++){
                if ((str.charAt(i))==(str.charAt(givenIndex))){
                    System.out.println(str.codePointBefore(i));
                    System.out.println(str.charAt(i-1));
                    break;
                }
            }
        } else {
            System.out.println("Given index is incorrect please select more than " +
                    "\n0 and less than or equal to "+ (len-1) +" only");
        }

    }
} // main block

/*Output----

    115
    s
*/