Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Basic java programing, nested for loops problem?
Alright, say I have a string that's guaranteed to be a length that's a multiple of 4 and it only has the letters A,B,C, and D in it (upper-case). say if I had a string "ABCDDCBA" I want to make an array [ABCD, DCBA]. I'm stuck on this, I figured it looks like a nested for loop problem?
I also think substrings is apart of it.
public static void arrayProb(String line){
String[] list = new String[line.length()/4];
for(int j = 0; j <= list.length - 1; j++){
for (int i = 0; i <= list.length - 4; i = i + 4){
list[I] = noDash.substring(i , i+4);
}
}
}
This is what I started with but for obvious reasons I have nulls in every index of the array that isn't 0 or a multiple of 4.
2 Answers
- heyy =]Lv 64 years agoFavorite Answer
you can do it with a nested for loop. But it's not neccesary, especially because java has the substring() method. Here's one solution without.
String line = "ABCDDCBA";
String[] list = new String[line.length()/4];
for(int j = 0; j <= list.length - 1; j++){
list[j] = line.substring(j*4, j*4+4);
}
- Anonymous4 years ago
U