1

For example, String letters = "fourgooddogsswam";

Is there a way I can scan the string from left to right 4 characters at a time so that I can set(four ourg urgo rgoo good oodd oddo ddog dogs ogss gssw sswa swam) into a string array? I tried using loops but I'm having difficulties getting it to work properly.

Thanks!

public static String[] findWordsOfLength(String letters, int wordSize) {
    if(letters == null) {
        return null;
    }

    int size = letters.length();
    int wordMax = size - wordSize + 1;
    if(size < wordMax || wordMax <= 0) {
        return new String[0];
    }

    int j = 0;
    String[] result = new String[wordMax];

    for (int i = 0; i < wordMax; i++) {
        result[j ++] = letters.substring(i, i + wordSize);
    }

    return result;
}
Billy
  • 19
  • 4
  • 3
    Show us what you've tried and explain where you're stuck. – shmosel Mar 16 '17 at 01:16
  • 1
    Please show the loops that you tried. SO is not a free coding service. – Mad Physicist Mar 16 '17 at 01:16
  • I put what I was working on, basically I bring in a string of letters and use the wordSize int to determine how many letters it scans the string for, for each word. the fourgooddogsswam example used 4 – Billy Mar 16 '17 at 01:20

2 Answers2

2

Use a while loop and arraylist like this,

    String hello = "fourgooddogsswam"; 

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

    int i = 0;
    while (i + 4 <= hello.length()) {

        substrings.add(hello.substring(i, i + 4));
        i++;

    }

    for (String s : substrings) {

        System.out.println(s);

    }

If you want to do this without arraylist just make a string array with size YOURSTRING.length() - (WHATEVERSIZE - 1);

Example

    String hello = "fourgooddogsswam"; 

    String[] substrings = new String[hello.length() - 3];

    int i = 0;
    while (i + 4 <= hello.length()) {

        substrings[i] = hello.substring(i, i + 4);
        i++;

    }

    for (String s : substrings) {

        System.out.println(s);

    }
Naberhausj
  • 303
  • 1
  • 9
  • Is there a way to do this without ArrayLists? – Billy Mar 16 '17 at 01:21
  • When trying to use this in a method, I return substrings but it gives me this as an output: [Ljava.lang.String;@15db9742 instead the array values. – Billy Mar 16 '17 at 01:33
  • So I would need to return a string array? – Billy Mar 16 '17 at 01:46
  • Whatever you need for this purpose, Do you want an array? Then yes. Do you want a string with all of the substrings gathered? Then see the above link. – Naberhausj Mar 16 '17 at 04:01
1

Just another way to do this.

import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test {

 public static void main(String args[]) {
  String str = new String("fourgooddogsswam");

  Pattern pattern = Pattern.compile(".{4,4}");
  Matcher matcher = pattern.matcher(str);

  while (matcher.find()) {
   System.out.println(matcher.group(0));
  }
 }
}

Will print:

four
good
dogs
swam

P.S. Yes, we all hate regex... but it does the work ;)

Alexander R.
  • 1,756
  • 12
  • 19