-4

I would like to make an 'infinite' list of strings for an application. I attempted to use this....

public static void runinfinite(int length) {
    String lastString= " ";
    while (true) {
        if (lastString.length() > length)
            break;
        lastString = lastString.trim();
        for (char c0 = 'a'; c0 <= 'z'; c0++) {
            lastString += c0;
            action(lastString);
        }
    }
}

It only gives me this: abcdefghijklmnopqrstuvwxyz. Does anyone have a block of code that will make strings 'infinitely'?

Edit: I need it to make a list of strings like this example: [doesn't need to be in the same order, just a bunch of strings]

Edit 2: I want to make this example work better, and run forever.

Nick Grealy
  • 24,216
  • 9
  • 104
  • 119
Joshua Katz
  • 11
  • 2
  • 4

3 Answers3

1

if you want to make a string of infinite length, you will eventually receive a stackoverflow exception.

likewise:

string badString = "";
Random random = new Random();
while(true)
{
  int asciiNum = random.nextInt(25) + 97;
  badString = badString + Character.toChars(asciiNum);
}

will result in badString consisting of an infinite number of a's, but the program will crash due to a out of memory error.

Please see this for more info:

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Character.html#toChars%28int%29 http://docs.oracle.com/javase/6/docs/api/java/util/Random.html - random class

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
tehdoommarine
  • 1,868
  • 3
  • 18
  • 31
  • I would like it to be a random string like this.... a, than aa, than aaa, [a bunch of these] than ab, abb, abbb so on and so on – Joshua Katz Sep 08 '12 at 20:53
  • you will want to use the random class then. produce a random int from 97 -122 (lower case a-z ascii values). convert to char and add to string. – tehdoommarine Sep 08 '12 at 20:55
  • 3
    Nitpick: it would likely be an [`OutOfMemoryError`](http://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.html) rather than a [`StackOverflowError`](http://docs.oracle.com/javase/7/docs/api/java/lang/StackOverflowError.html). – Paul Bellora Sep 08 '12 at 21:11
  • 3
    Also, instantiating and using a new `Random` within each iteration of a loop is a common mistake - make a single `Random` outside the loop and continue to use it. – Paul Bellora Sep 08 '12 at 21:16
  • 1
    @PaulBellora - you are correct. answer has been edited and comment upvoted – tehdoommarine Sep 08 '12 at 21:18
  • I dont want an a long string, I want a bunch of strings with text like the pastebin I have linked. – Joshua Katz Sep 08 '12 at 21:55
  • @JoshuaKatz "a then aa then aaa" isn't random (well, it could be). It looks more like you're trying to create permutations of varying lengths over a bounded character set. There are like a million examples of brute-force password generators. – Dave Newton Sep 08 '12 at 22:31
0

if you want to run infinitely, why do you break the loop then?

if (lastString.length() > length)
    break; // <--- it stops your "infinite" loop

what is the meaning of this line?

lastString = lastString.trim();

only thing it does - removes initial space from the very first assignment since you never populate it with whitespace characters. you could as easily use an empty string from the beginning.

it stops after 'z' simply because you probably pass a value less than 26 as your length parameter when calling the function. otherwise it would stop after next 'z' that would make string longer than whatever number you pass in.

upd: you explained it's for max length. then how is it "infinite"? or you want an "infinite" list of strings consisting of characters 'a' through 'z'? or an "infinite" list of the following pattern:

for length = 10:
list = [
"a",
"ab",
"abc",
...
"abcdefghij", // reached max length
"a",
"ab",
...
]
aiodintsov
  • 2,545
  • 15
  • 17
  • The 'length' is the max length of the strings. – Joshua Katz Sep 08 '12 at 20:54
  • @JoshuaKatz it makes 26 of them, "a" through "ab....z". try debugging through to understand why. you also refuse to explain what you try to achieve so it's not very easy to help on this. – aiodintsov Sep 10 '12 at 14:19
0

See my comment. It appears you want a stream.

Here's an example...

static final Iterable<Character> alphabet() {
  return new Iterable<Character>() {

    private final char[] ALPHA = new char[] {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    };

    public final Iterator<Character> iterator() {
      return new Iterator<Character>() {

        private int cursor;

        public boolean hasNext() {
          return true;
        }

        public Character next() {
          char ch = ALPHA[cursor];
          cursor = (cursor + 1) % 26;
          return ch;
        }

        public void remove() {
          throw new UnsupportedOperationException("cannot remove from stream");
        }
      };
    }
  };
}

... coupled with:

for (final char ch : alphabet()) {
  System.out.print(ch);
}
Community
  • 1
  • 1
obataku
  • 29,212
  • 3
  • 44
  • 57
  • @JoshuaKatz I didn't try to reimplement your approach, it's merely an example to demonstrate... :-p – obataku Sep 08 '12 at 21:23
  • No, that is not what I wanted, I want to make a list of strings. So make a string "a" and add it to a list. And make the next string "aa". I am seeing if I can make a crypto method better than MD5 and for that I need to see how long it takes to break MD5 [as in hash a bunch of things]. I just need to infa-gen strings for hashing – Joshua Katz Sep 08 '12 at 21:29
  • 2
    @JoshuaKatz You *do* want an iterator, otherwise if you generate an "infinite number of strings" you will run out of memory. There is zero reason to *keep* the strings you generate. You want an iterable permutation generator. – Dave Newton Sep 08 '12 at 22:32