charAt in method not working

The TwistyTie

here is the method:

    public static String getEncrypted(String x){
    char y;
    String output = " ";
    int count = 0, position = 0, place= 0;
    int length = x.length();
    while(count < length){
        place = length - position;
        y = x.charAt(place);
        position++;
        count++;
        output = output + y;
    }
    return output;
}

I am able to compile the code which basically takes a string splits it up into the words that make it then feeds it to this method.

When I use the program and enter something to be used by this method I get the error message

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
    at java.lang.String.charAt(Unknown Source)
    at encrypt.getEncrypted(encrypt.java:37)
    at encrypt.main(encrypt.java:23)

My question is what I am doing wrong and how do I fix it?

Jo Kachikaran

Change place = length - position; to place = length - position - 1; and try again

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

String method in Java: charAt()

From Dev

Mockito when method not working

From Dev

Imported struct method not working

From Dev

rails .order method not working

From Dev

Custom Init Method not working

From Dev

charAt is not a function

From Dev

String's CharAt method using long

From Dev

charAt method, exam paper Q

From Dev

Method is not working

From Dev

Jquery post method not working

From Dev

javascript first() method not working

From Dev

Creating a CharAt() method in the main class

From Dev

errors with charAt method in javascript

From Dev

Npm: Cannot call method 'charAt' of undefined

From Dev

Avoid String charAt() method inside loops

From Dev

Validation method not working as intended

From Dev

Kotlin: Method reference not working?

From Dev

Laravel hasMany method not working

From Dev

is there anything similar to Java's string 'charAt()' Method in C?

From Dev

pry inspect method not working

From Dev

How does the charAt() method work with taking numbers from strings and putting them into new strings in Java?

From Dev

Is there a way to combine the charAt() method and includes() method instead of having to specify a certain character for charAt()?

From Dev

charAt method, exam paper Q

From Dev

Npm: Cannot call method 'charAt' of undefined

From Dev

Simple javascript if statement with charAt is not working

From Dev

Is there an opposite method for .charAt()?

From Dev

"(firstName.toLowerCase()).charAt(0)" in this manner with Java? the charAt() method should only work with a string reference variables?

From Dev

Error : Symbol not found in java method charAt(int)

From Dev

charAt() not working second time in function for javascript

Related Related

HotTag

Archive