You could use Guava to help simplify the effort to handle the string transformations and to stitch the results together:
package testCode;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
public class TestMain {
static Joiner joinWithComma = Joiner.on(", ").skipNulls();
private static String getCourses(Iterable<String> fromArray) {
return joinWithComma.join(Iterables.transform(fromArray,
new Function<String, String>() {
@Override
public String apply(String arg0) {
return arg0.substring(0, arg0.length() - 1);
}
}));
}
private static String getGrades(Iterable<String> fromArray) {
return joinWithComma.join(Iterables.transform(fromArray,
new Function<String, String>() {
@Override
public String apply(String arg0) {
return arg0.substring(arg0.length() - 1, arg0.length());
}
}));
}
public static void main(String[] args) {
String[] arr = { "CSE01A", "ECE02B", "MECH03C" };
System.out.println(getCourses(Lists.newArrayList(arr)));
System.out.println(getGrades(Lists.newArrayList(arr)));
}
}
Output:
CSE01, ECE02, MECH03
A, B, C
I have a feeling, though, that this is way over the top for what might be a simple homework assignment on iterating through arrays and string manipulation.