java – Increase a char value by one-ThrowExceptions

Exception or error:

I have an app with an EditText and a button named “forward”. All I want is just when I type “a” in the EditText and click the button, the word “b” to be typed in the EditText, “c” if pressed again and so on. I have tried:

value = edt.getText();
edt.setText(value + 1);

But that of course will print the initial string followed by the number “1”. Any ideas? Thanks a lot

How to solve:

Tested and works

All letters can be represented by their ASCII values.

If you cast the letters to an int, add 1, and then cast back to a char, the letter will increase by 1 ASCII value (the next letter).

For example:

'a' is 97
'b' is 98

So if the input was 'a' and you casted that to an int, you would get 97. Then add 1 and get 98, and then finally cast it back to a char again and get 'b'.

Here is an example of casting:

System.out.println( (int)('a') ); // 97
System.out.println( (int)('b') ); // 98
System.out.println( (char)(97) ); // a
System.out.println( (char)(98) ); // b

So, your final code might be this:

// get first char in the input string
char value = et.getText().toString().charAt(0);

int nextValue = (int)value + 1; // find the int value plus 1

char c = (char)nextValue; // convert that to back to a char

et.setText( String.valueOf(c) ); // print the char as a string

Of course this will only work properly if there is one single character as an input.


A simpler way would be:

char value = edt.getText().toString().charAt(0);
edt.setText(Character.toString ((char) value+1));

Here the value + 1 adds the decimal equivalent of the character and increments it by one.. Here is a small chart:

enter image description here
enter image description here

Whats happens after ‘z’? … it wont crash.. see here for the full chart..


Try this one

String convertString = getIncrementStr("abc");
public static String getIncrementStr(String str){
    StringBuilder sb = new StringBuilder();
    for(char c:str.toCharArray()){
    return sb.toString();


String value = edt.getText();
edt.setText(value + 1);

If the above is your code, then what you are doing is concatenating a 1 to the end of your value string. Thus, a button click would alter the displayed text from “your text” to “your text1”. This process would continue on the next button click to show “your text11”. The issue is really a type error.


You need to convert the text value (character) to the ascii value, then increase it, not just the text.

value = edt.getText().toString();
int ascii = (int)value;
edt.setText(Character.toString ((char) ascii+1));


Try this:

edt.setText(Character.toString((char)(edt.getText().charAt(0) + 1)));

Leave a Reply

Your email address will not be published. Required fields are marked *