Create secret messages The problem is stated in Encrypt this!:

Your message is a string containing space separated words. You need to
encrypt each word in the message using the following rules:

The first letter needs to be converted to its ASCII code.

The second letter needs to be switched with the last letter

Keepin' it simple: There are no special characters in input.


Kata.encryptThis(&quot;Hello&quot;) =&gt; &quot;72olle&quot;
Kata.encryptThis(&quot;good&quot;)` =&gt; &quot;103doo&quot;
Kata.encryptThis(&quot;hello world&quot;) =&gt; &quot;104olle 119drlo&quot;

class NewEnpc{
    public static String encryptThis(String text) {
        StringBuilder sb=new StringBuilder();
        String[] arry=text.split(&quot; &quot;);
        if(text!=&quot;&quot;){
            for(int j=0;j&lt;arry.length;j++){
                char[] sd=arry[j].toCharArray();
                int number=(int)sd[0];
                if(sd.length&gt;1){
                    for(int k=0;k&lt;sd.length;k++){
                        if(k==0){
                            sb.append(number);
                        }
                        else if(k==1){
                            int len=sd.length-1;
                            char c=sd[len];
                            sb.append(c);
                        }
                        else if(k==sd.length-1){
                            char c=sd[1];
                            sb.append(c);
                        }
                        else{
                            char c=sd[k];
                            sb.append(c);
                        }
                    }
                }
                else{
                    sb.append(number);
                }
                sb.append(&quot; &quot;);
            }
        }
        return sb.toString().trim();
    }
}

public class EncryPt {
    public static void main(String[]args){
        System.out.println(&quot;Hello&quot;);
         
      System.out.println(NewEnpc.encryptThis(&quot;A 
        wise old owl lived in an oak&quot;));
    }
}

Can someone help me improve my coding standards. I have solved the problem.
I would suggest following:


Do not compare strings by ==, use String.equals(). And in Your case is better String.isEmpty()

text != "" &rarr; !text.isEmpty()
Use constants for numbers and strings. It increases readability and futher refactoring.

private static final String WORD_SEPARATOR = " ";
There is no need to use indexed iterations

for(int j=0;j&lt;arry.length;j++) &rarr; for (String s : arry)
Use IDE with spelling checker:

arry &rarr; array
Use meaning variable names.

String s &rarr; String word
No need to use cast:

int number = (int) sd[0]; &rarr; int number = sd[0];
Decrease level of visibility

public static String encryptThis &rarr; static String encryptThis
Use method extraction to decrease nesting level

private static StringBuilder encrypt(char[] wordChars, int number)
You add first char in any case so condition can be omitted.
You could use chain of method .append(...) calls for StringBuilder
It is nice to move conditions like k==1 to meaningful named method:

See isSecondChar(...)
It is nice to validate input params early and prevent any actions if it is not valid:

if(text!="") &rarr; if (text.equals("")) then return empty string;


I would strongly suggest you to read great Joshua Bloch 'Effective Java' book. 

Have a look at the refactored code:

class NewEnpc {

    private static final String WORD_SEPARATOR = " ";

    static String encryptThis(String text) {
        if (text.isEmpty()) {
            return text;
        }
        StringBuilder result = new StringBuilder();
        String[] words = text.split(WORD_SEPARATOR);
        for (String word : words) {
            char[] wordChars = word.toCharArray();
            result.append((int) wordChars[0])
                    .append(encrypt(wordChars))
                    .append(" ");
        }
        return result.toString().trim();
    }

    private static StringBuilder encrypt(char[] wordChars) {
        StringBuilder encrypted = new StringBuilder();
        for (int index = 1; index &lt; wordChars.length; index++) {
            if (isSecondChar(index)) {
                int len = wordChars.length - 1;
                encrypted.append(wordChars[len]);
            } else if (isLastChar(index, wordChars.length - 1)) {
                encrypted.append(wordChars[1]);
            } else {
                encrypted.append(wordChars[index]);
            }
        }
        return encrypted;
    }

    private static boolean isLastChar(int currentIndex, int prevCharIndex) {
        return currentIndex == prevCharIndex;
    }

    private static boolean isSecondChar(int currentIndex) {
        return isLastChar(currentIndex, 1);
    }
}

public class EncryPt {
    public static void main(String[] args) {
        System.out.println("Hello");
        System.out.println(NewEnpc.encryptThis("A wise old owl lived in an oak"));
    }
}