How can I alter this Java method so that I convert and then return every sentenc
ID: 3845816 • Letter: H
Question
How can I alter this Java method so that I convert and then return every sentence in the array of strings to an array of chars? As of now I can only get the first sentence into the char array but I need all 5 sentences.
public static char[] charConvert ( String [] sentences ) {
int totLength = sentences[0].length() + sentences[1].length() + sentences[2].length() + sentences[3].length() + sentences[4].length();
char [] letter = new char[totLength];
int x = 0;
int y = 0;
while ( y < sentences[x].length() ) {
switch ( sentences[x].charAt(y) ) {
case 'a': case 'A':
letter[y] = 'N';
break;
case 'b': case 'B':
letter[y] = 'O';
break;
case 'c': case 'C':
letter[y] = 'P';
break;
case 'd': case 'D':
letter[y] = 'Q';
default: break;
}
y++;
}
return letter;
}
Explanation / Answer
public static char[] charConvert(String[] sentences) {
int totLength = sentences[0].length() + sentences[1].length() + sentences[2].length() + sentences[3].length()
+ sentences[4].length();
char[] letter = new char[totLength];
int x = 0;
int y = 0, z = 0;
for (int i = 0; i < sentences.length; ++i) {
String str = sentences[i];
z = 0;
while (y < totLength && z < str.length()) {
switch (str.charAt(z)) {
case 'a':
case 'A':
letter[y] = 'N';
break;
case 'b':
case 'B':
letter[y] = 'O';
break;
case 'c':
case 'C':
letter[y] = 'P';
break;
case 'd':
case 'D':
letter[y] = 'Q';
default:
break;
}
y++;
z++;
}
}
return letter;
}
=================================================================================
Thanks, let me know if there is any concern. I will be happy to modify and make it work for you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.