-1

I have a Java string that contains numbers and words interchanged throughout as follows:

String str = "7 first 3 second 2 third 4 fourth 2 fifth"

I need to find a concise way (if possible) to print the words (first, second, third, fourth, fifth, etc.) the number of times shown.

The expected output is:

firstfirstfirstfirstfirstfirstfirst
secondsecondsecond
thirdthird
fourthfourthfourthfourth
fifthfifth

I tried to split the the string into an array and then use a for loop to iterate over every other number (in my outer loop) and every other word in my inner loop, but I was not successful.

Here is what I tried but I suspect this is not the correct approach or at the very least not the most concise one:

String[] array = {"7", "first", "3", "second", "2", "third", "4", "fourth", "2", "fifth"};

for (int i=0; i < array.length; i+=2)
{
   // i contains the number of times (1st, 3rd, 5th, etc. element)

   for (int j=1; j < array.length; j+=2)

   // j contains the words (first, second, third, fourth, etc. element)    
       System.out.print(array[j]);

}

I'll be the first to admit I am very much a notice at Java, so if this approach is completely asinine please feel free to laugh, but thank you in advance for your assistance.

Kurt W
  • 321
  • 2
  • 15
  • You definitely do not want a nested loop. This gives you every combination of number and word. So you'd get 7x `first`, 7x `second` 7x `third`, ..., 3x `first`, 3x `second`, and so on. Instead, note that when `i` is 0, the only word you're concerned with is at `j=1`. When `i` is 2, the only word you're concerned with is `j=3`. So start by taking out the inner for loop, and just using `i+1` as the index of the word. – ajb Jun 12 '16 at 02:07
  • To clarify: you do not want a nested loop where both loops go through the array. TDG's answer has a nested loop but that's for a different purpose. – ajb Jun 12 '16 at 02:13
  • @Jarrod Roberson - I don't think it's a duplicate, since there is another issue here besides converting a string to int: the OP is using a nested loop in a wrong way, so even if he gets the int somehow, he won't get the desired output. – TDG Jun 12 '16 at 02:30

2 Answers2

1

Considering your solution, the main problem consists in that you don't need to iterate considering the initial string array inside the inner loop. Rather, you should read the numeric digit and iterate considering it as a limit. As follows, for example:

    String initialString = "7 first 3 second 2 third 4 fourth 2 fifth"; 
    String splittedStrings[] = initialString.split(" ");
    for(int i = 0; i < splittedStrings.length; i = i + 2){
        int times = Integer.parseInt(splittedStrings[i]);
        String number = splittedStrings[i+1]; 
        for(int j = 0; j < times; j++){
            System.out.print(number);
        }
        System.out.println();
    }

Hope it helps!

Lorenzo Addazi
  • 325
  • 3
  • 12
  • Totally makes sense! I forgot to show in my code that I was using `parseInt` but your answer covered it regardless. I'll test this shortly. – Kurt W Jun 12 '16 at 02:30
  • Thanks a lot Lorenzo! Since both of the answers were corrected but the above answer came in first and was equally as concise, so I went with it. I did upvote yours though. Sorry, I wanted to mark yours correct but only one can be :| – Kurt W Jun 12 '16 at 03:46
  • Yes, I guess we answered almost at the same time. No problem :) The important thing is that you solved your problem! Thank you for the upvote! – Lorenzo Addazi Jun 12 '16 at 14:00
  • :-) -- exactly right. Would prefer to give it to you since your ranking is lower, but such is life. Thanks for understanding! – Kurt W Jun 14 '16 at 00:13
0

Parse the number as an int, and then use this value to print the word as many times as you need:

String[] array = {"7", "first", "3", "second", "2", "third", "4", "fourth", "2", "fifth"};
for (int i=0; i < array.length; i+=2)
{
   int count = Integer.parseInt(array[i]);
   for (int j=0; j < count; j++) {
       System.out.print(array[i+1]);
   }
   System.out.println();
}

count will get the values 7, 3, 2, etc.

TDG
  • 5,909
  • 3
  • 30
  • 51