How can I alter this Java method so that I convert and then return every sentence in the array of strings instead of just the first sentence?
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++;
}
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;
}
I can help you in altering this Java method in many different ways. I have developed two programs that return every sentence in the array of strings.
a) Let me describe you about the first program which demonstrates the use of ArrayList to convert string into Array:-
import java.util.ArrayList;
import java.util.List;
public class charCount {
public static void main(String[] args) {
String str = "N,O,P,Q";
String[] records = str.split(",");
List<String> recordList = new
ArrayList<String>();
for (String result: records ) {
recordList.add(result);
}
System.out.println(recordList);
}
}
The split() method of the String class separates a String into an array of substrings. Here, It helps us to convert the output of the Java String separates, which is a String array, to an ArrayList.
b) Let me brief you about the second program which demonstrates the use to convert String to Char Array:
public class convertStringArr {
public static void main(String[] args) {
String str = "convert Sentences
into Array";
char[] stringToCharArray =
str.toCharArray();
for (char result :
stringToCharArray) {
System.out.println(result);
}
}
}
It basically converts the string into character array.It is using the for-each method which starts from the beginning till the end.
Get Answers For Free
Most questions answered within 1 hours.