Skip to main content

Command Palette

Search for a command to run...

Java program: Get character before index

Updated
1 min read
Java program: Get character before index
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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
*/

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.