Question:

Hey guyz. I'm writing a java code and...?

by  |  earlier

0 LIKES UnLike

Hey guyz. I'm writing a java code and i wanna know if there's a shorter way to write this code below. thanks.

for(i = 0; i < s.length(); i++)

{

aChar = s.charAt(i);

switch(aChar)

{

case'a': aChar = theKey.charAt(0); break;

case'A': aChar = theKey.toUpperCase().charAt(0); break;

case'b': aChar = theKey.charAt(1); break;

case'B': aChar = theKey.toUpperCase().charAt(1); break;

case('c'): aChar = theKey.charAt(2); break;

case('C'): aChar = theKey.toUpperCase().charAt(2); break;

case('d'): aChar = theKey.charAt(3); break;

case('D'): aChar = theKey.toUpperCase().charAt(3); break;

case('e'): aChar = theKey.charAt(4); break;

case('E'): aChar = theKey.toUpperCase().charAt(4); break;

case('f'): aChar = theKey.charAt(5); break;

case('F'): aChar = theKey.toUpperCase().charAt(5); break;

case('g'): aChar = theKey.charAt(6); break;

case('G'): aChar = theKey.toUpperCase().charAt(6); break;

case('h'): aChar = theKey.charAt(7); break;

case('H'): aChar = theKey.toUpperCase().charAt(7); break;

case('i'): aChar = theKey.charAt(8); break;

case('I'): aChar = theKey.toUpperCase().charAt(8); break;

case('j'): aChar = theKey.charAt(9); break;

case('J'): aChar = theKey.toUpperCase().charAt(9); break;

case('k'): aChar = theKey.charAt(10); break;

case('K'): aChar = theKey.toUpperCase().charAt(10); break;

case('l'): aChar = theKey.charAt(11); break;

case('L'): aChar = theKey.toUpperCase().charAt(11); break;

case('m'): aChar = theKey.charAt(12); break;

case('M'): aChar = theKey.toUpperCase().charAt(12); break;

case('n'): aChar = theKey.charAt(13); break;

case('N'): aChar = theKey.toUpperCase().charAt(13); break;

case('o'): aChar = theKey.charAt(14); break;

case('O'): aChar = theKey.toUpperCase().charAt(14); break;

case('p'): aChar = theKey.charAt(15); break;

case('P'): aChar = theKey.toUpperCase().charAt(15); break;

case('q'): aChar = theKey.charAt(16); break;

case('Q'): aChar = theKey.toUpperCase().charAt(16); break;

case('r'): aChar = theKey.charAt(17); break;

case('R'): aChar = theKey.toUpperCase().charAt(17); break;

case('s'): aChar = theKey.charAt(18); break;

case('S'): aChar = theKey.toUpperCase().charAt(18); break;

case('t'): aChar = theKey.charAt(19); break;

case('T'): aChar = theKey.toUpperCase().charAt(19); break;

case('u'): aChar = theKey.charAt(20); break;

case('U'): aChar = theKey.toUpperCase().charAt(20); break;

case('v'): aChar = theKey.charAt(21); break;

case('V'): aChar = theKey.toUpperCase().charAt(21); break;

case('w'): aChar = theKey.charAt(22); break;

case('W'): aChar = theKey.toUpperCase().charAt(22); break;

case('x'): aChar = theKey.charAt(23); break;

case('X'): aChar = theKey.toUpperCase().charAt(23); break;

case('y'): aChar = theKey.charAt(24); break;

case('Y'): aChar = theKey.toUpperCase().charAt(24); break;

case('z'): aChar = theKey.charAt(25); break;

case('Z'): aChar = theKey.toUpperCase().charAt(25); break;

}

aString += aChar;

}

 Tags:

   Report

1 ANSWERS


  1. Yes, there is.

    First, use Character.toUpperCase instead of converting the entire string to uppercase every time you encounter an uppercase character.

    Second, you can use some character arithmetic to simplify it a bit more.

    Third, tertiary-ifs are awesome, and should be used at every opportunity.

    Fourth, rewriting 85-lines of code as one statement is cool and should also be done at every opportunity, even if the result is neigh unreadable. Trust me, I&#039;ve been doing this for a while.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.