0

Okay so i'm not sure how to go about creating a string from a bunch of string arrays. I have this method that takes in 3 string arrays

public static String formatStory(String[] shortest, String[] medium, String[] longest){

    return formattedStory;
}

I want to take a long word, a medium word and a short word and add it to a string and then repeat the loop to do it all over again until theres no words left in the string Arrays. Would I use a while loop since I dont know how many iterations it would take? Or for loops?

Jess
  • 11
  • 2
  • What language is this? I'm guessing C#? You should really tag your questions with a language so the appropriate experts can respond to it. Also, beyond just writing the method's signature, what have you tried to do? Have you attempted anything else to solve it? – Sean Werkema Mar 16 '17 at 03:39

6 Answers6

0

Use while, but make sure in each iteration you have all 3 strings values. This is a very high memory and cpu usage function, may be you should think how to optemize it a bit.

I hope that helps.

YCotov
  • 92
  • 6
  • There is no difference in java between a `while()` and a `for()` loop. the bytecode is the same: http://stackoverflow.com/a/8880898/4985572 – Igoranze Mar 16 '17 at 10:39
0

You can use a for loop as well.

String s = "";
for (int i = 0; i < shortest.length; i++) {
  s = s + longest[i] + medium[i] + shortest[i];
}
return s;
Igoranze
  • 1,506
  • 13
  • 35
Adeel
  • 413
  • 1
  • 7
  • 22
0

It's not that simple and I'm not sure it can be solved using only one loop. For Me it took two functions and java equivalent of foreach-loop to solve the task. Also, I suggest to use Lists. First you need to group words from three arrays by positions. And second, resulted strings have to be concatenated into one. Check the code below.

import java.util.*;

/*Some code...*/

private static void insertArrayIntoList(List<String> items, String[] array) {
    if(array == null) return;
    int count = 0;
    for(String item : array) {
        if(items.size() > count) {
            String temp = items.get(count);
            temp += " " + item;         
            items.set(count, temp);
        } else {
            items.add(item); 
        }
        count++;
    }
}
public static String formatStory(String[] shortest, String[] medium, String[] longest){ 

    List<String> items = new ArrayList<String>();

    insertArrayIntoList(items, shortest);
    insertArrayIntoList(items, medium);
    insertArrayIntoList(items, longest);

    String formattedStory = "";
    for(String item : items) {
       formattedStory += ((formattedStory.length() == 0) ? "" : " ") + item;                          
    }
    return formattedStory;
}
0

If you are not sure which array is the shortest you could check it first, than after you find this out you can make a for loop so that you always get the value from all three arrays with the lowest value of the weakest link... like so:

 private static void doStackOverflow() {
     String[] shortest = {"one", "two", "three", "four"};
     String[] medium = {"a", "b", "c", "d", "e", "f"};
     String[] longest = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};

     System.out.println(formatStory(shortest, medium, longest));
 }

 public static String formatStory(String[] shortest, String[] medium, String[] longest) {

     int weakestLink = findWeakestLink(shortest.length, medium.length, longest.length);

     String formattedStory = "";
     for (int i = 0; i < weakestLink; i++) {
         formattedStory += shortest[i] + " " + medium[i] + " " + longest[i] + " ";
     }

     return formattedStory;
 }

 private static int findWeakestLink(int shortest, int medium, int longest) {
     int result = shortest + medium + longest;

     if (shortest < result)
         result = shortest;
     if (medium < result)
         result = medium;
     if (longest < result)
         result = longest;

     return result;
 }
Igoranze
  • 1,506
  • 13
  • 35
0

A "quick and dirty" (imo) solution would be this:

public static String formatStory(String[] shortest, String[] medium, String[] longest) {

    int s = shortest.length;
    int m = medium.length;
    int l = longest.length;

    int max = Integer.max(s, Integer.max(m, l));

    StringBuilder result = new StringBuilder();

    for (int i = 0; i < max; i++) {
        if (i < l)
            result.append(longest[i]).append(" ");
        if (i < m)
            result.append(medium[i]).append(" ");
        if (i < s)
            result.append(shortest[i]).append(" ");
    }

    return result.toString().trim();
}

Which simply takes the length of the biggest array, loops (for loop for index) over all arrays, looks, if there is still an element left in the array (index < length) and appends it. You can also use String and simply append it with + if this fits your style more.

Pascal Schneider
  • 405
  • 1
  • 5
  • 19
-1

You could use for-loop or while-loop since both the loops would take a condition which is same which would be the length's of the arrays. One loop will work only if you have all the three string arrays are of same size, else you have to write it in different ways for optimized performance. Also use StringBuilder instead of concatenation for better memory performance.

Sampath
  • 599
  • 5
  • 12
  • There is no difference in Java between a `while()` and a `for()` loop. the bytecode is the same: http://stackoverflow.com/a/8880898/4985572. Generally using JDK 1.6 and above the compiler will automatically join strings together using StringBuilder. But using `StringBuilder` in loops is indeed better! http://stackoverflow.com/a/14927864/4985572 – Igoranze Mar 16 '17 at 10:46