1
\$\begingroup\$

I made a variation on the program that moves the character to the next character. This version moves the characters n steps forward and takes care of white spaces in between. It works but wonder if there is a better way of coding this. It only works with lowercase alphabet characters.

import java.util.Scanner;
public class NextCharacters{
    public static char nextCharacterInAlphabet(char character , int step)
    {
           char nextChar;           
           int ascii = (int) character;

           if (ascii ==32)  //ascii code for a space is 32
                     /*space stays space that separates the different 
                     strings seperated by the spaces */
                   nextChar = (char) ascii;   
           /*at ascii 104=h remainder goes to zero so i have to add 104, have to add
           104 till ascii=122=z*/
            else if ((ascii +step) % 26>=0 && (ascii +step) % 26 <=18)
            {
                nextChar = (char) ( ((ascii +step) % 26)+104);
            }
            else
                    nextChar = (char) ( ((ascii +step) % 26)+78);
           /*first character is the 'a' with ascii value 97. Remainder of 97%26 is 19
           so to come up with 97 you have to add 78 to get the value for a.
           The same applies if you increase the number of positions you want the
           chars to move forward, have to this till it reaches ascii =104 where 
           the remainder gets to 0 and have to add 104*/
        return nextChar; 
    }

    public static void main(String[] args) {

        Scanner inputChars = new Scanner( System.in );

        Scanner inputSteps = new Scanner( System.in );

        StringBuilder sb = new StringBuilder();

        System.out.println("Please enter the characters ");
        String characterString = inputChars.nextLine();

        System.out.println("Please enter the number of positions you want each character to move forward");
        int numberOfMovesForward = inputSteps.nextInt();
        for (char c : characterString.toCharArray() )
        {
            sb.append(nextCharacterInAlphabet(c,numberOfMovesForward ));
        }
        System.out.println(sb.toString()) ;

    }

}
\$\endgroup\$
5
  • \$\begingroup\$ Try using the toUppercase() method to convert lowercase to uppercase characters. Use a || operator in your else if statement to cover both case conditions. \$\endgroup\$
    – sawreals2
    Dec 17, 2016 at 0:45
  • \$\begingroup\$ Consider rewriting your comments and magic numbers as code--and use literal characters such as ' ' instead of 32. \$\endgroup\$
    – Tom Blodget
    Dec 17, 2016 at 1:33
  • \$\begingroup\$ Hi I tried to use characters instead of numbers, but it was easier to use numbers to figure out the math. The comments helped me to figure out the code. \$\endgroup\$
    – Shimrod
    Dec 17, 2016 at 9:08
  • \$\begingroup\$ Why do you need two scanners? \$\endgroup\$
    – Zhe
    Dec 17, 2016 at 14:36
  • \$\begingroup\$ i realised i only need 1 scanner object. My mistake was that i thought i needed a seperate object for each different variable. Thanks for making me aware of that \$\endgroup\$
    – Shimrod
    Dec 22, 2016 at 2:27

1 Answer 1

1
\$\begingroup\$

You should try to state your problem more clearly: "the program that moves the character to the next character" seem to assume we know some other program you're referring to. If I understand what you're trying to do, this is the problem:

"Given a String, replace each alphabetical character with the character STEP positions after in the alphabet, wrapping around if needed."

If this is the problem you're trying to solve, here is how I would do it:

public static char nextCharacterInAlphabet(char character , int step) {
  boolean isLower = Character.isLowerCase(character);
  char c = Character.toLowerCase(character);
  if (c < 'a' || c > 'z')
    return character;
  char nextC = (char)((c - 'a' + step) % 26 + 'a');
  return isLower ? nextC : Character.toUpperCase(nextC);
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks, that method worked excellent. Will study your code to figure it out . Just started coding again after 8 years so have to relearn most of it again \$\endgroup\$
    – Shimrod
    Dec 17, 2016 at 9:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.