1

I thought typecasting to char we do the trick but it just prints ascii values

   String str = JOptionPane.showInputDialog("Enter a word");

   str = str.toUpperCase();
   String temp = "";

   for(int i = 0 ; i < str.length() ; i++)
   {
       temp += (char)str.charAt(i) + 1;
   }

   System.out.println(temp);
6
  • str.charAt(i) + 1 is the likely culprit Commented Feb 5, 2017 at 7:01
  • 1
    I'm pretty sure it's implicit conversion from char to int. You're casting it to a char then adding 1 onto it. Put some brackets in. Commented Feb 5, 2017 at 7:01
  • Assuming you're trying shift to the next character, trying using temp += (char) (str.charAt(i) + 1);, which takes the char adds 1 to it (resulting in an int) and then casts the result back to char Commented Feb 5, 2017 at 7:03
  • Just to demonstrate the point , essentially u are doing something like temp += (int)str.charAt(i) + 1; Commented Feb 5, 2017 at 7:06
  • @sean what do you think the temp contains if str.charAt(i) is 'a'. Although OP wants the next alphabetic character not some int value. Commented Feb 5, 2017 at 7:16

2 Answers 2

1

Your mistake is you are adding integer 1 to char . This will return the ASCII code of the next char.

Change to

temp += (char)(str.charAt(i) +1)

Sign up to request clarification or add additional context in comments.

2 Comments

Correct, but why, I'd be nice to provide some background information to the OP so they understand better their mistake and not fall into the same trap again
these "change to" kind of things should be comment if they are short enough.
1

You are adding + 1 and that is causing it to be converted to integer I am not sure if explained properly just remove it

1 Comment

Sorry i forgot to mention in my question that i was trying to print the next char, but thanks for your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.