0

I have a project which requires me to insert a random number (between 0 and 9) after each letter in a word, is there any way to do this simply?

System.out.println(sent.replaceAll(".(?!$)", "$0 "));

I can get a space between each letter with this code but I'm not sure how to insert the numbers randomly

Input = Table
Output would = T8a9b6l2e4
default locale
  • 13,035
  • 13
  • 56
  • 62
Hexed
  • 1
  • 1
  • Could you give some examples of sample Input And Output. Without that, it's just pure guessing – dumbPotato21 Feb 01 '17 at 13:30
  • if you have a space between each letter, you can replace them with a random number – XtremeBaumer Feb 01 '17 at 13:31
  • Yes there is a way to do it easily, though probably not using a regex. Regular expressions are not good at generating random digits. Better to use some more standard Java code, where there is a class `Random` which can easily generate a random digit. – rossum Feb 01 '17 at 13:33
  • sent = sent.replaceFirst(" ", r); The problem is only this piece of code, Random cannot be converted to a string so how would I fix this (Netbeans) – Hexed Feb 01 '17 at 13:46
  • @Hexed That error is fixed now. Check the updated answer – dumbPotato21 Feb 01 '17 at 13:51

3 Answers3

1
String sent="a b c d e f";
Random r=new Random();
while(sent.contains(" ")){
    int f= random.nextInt(9-0+1)+0;
    sent=sent.replaceFirst(" ",String.valueOf(f));
}
System.out.println(sent);

this should work and if not it might give you an idea on how to solve it

Code for random int from: Generating a Random Number between 1 and 10 Java

Community
  • 1
  • 1
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • Thanks, it says there's an error on r.next(); it requires an int – Hexed Feb 01 '17 at 13:36
  • i updated my code a bit. only problem will be the r at the replacing. you will have to find something there – XtremeBaumer Feb 01 '17 at 13:39
  • 1
    `sent=sent.replaceFirst(" ",r);`, You are converting a `Random` object to `String` – dumbPotato21 Feb 01 '17 at 13:40
  • i know. i already said that there will be an error. i have now updated it. only thing to do is to limit the range of the ints generated – XtremeBaumer Feb 01 '17 at 13:43
  • Is there a space between each letter? Otherwise the easiest and fastest would be to just iterate through the whole thing and create a new string with letters from the input and digits from your random thingie. – Octav Zlatior Feb 01 '17 at 13:46
  • i found this code smoother. might be that you are right. yes there are spaces between each letter, because op was able to insert a space between each letter – XtremeBaumer Feb 01 '17 at 13:49
  • Haha, I see what you mean. Then it makes sense if you put it that way, but it does make the space insertion thing a bit redundant. I like the replaceFirst() idea :) – Octav Zlatior Feb 01 '17 at 13:51
  • Sorry, didn't see that. Yeah, and I have a question for another task. Can anyone help me write a code to determine if the inputted number is an ordered number (if the first half of the number minus the second half = 1 e.g. 123122; 123-122 = 1) and to also find out if the ordered number is an ordered square (An ordered square is an ordered number that is a perfect square e.g. 8281) – Hexed Feb 01 '17 at 13:52
  • write a new question for this and try to make it more clear. i as a non Englishmen don't understand what your question is. maybe other do – XtremeBaumer Feb 01 '17 at 13:54
  • @Hexed Also do some research before asking, or else your question would be closed as a **HomeWork question** – dumbPotato21 Feb 01 '17 at 13:55
  • as an English speaker I don't understand it, I have done research. Can't find anything.I can only post a question every 90 minutes. So: If a first half of a number minus the second half of the number = 1 it is an Ordered number. If the same number has an Integer square root it is an Ordered Square. E.g. 8281 is an ordered number and square. I have attempted a code to allow a user to input a number and the number is checked to see if it an Ordered square and prime and then prints out if the number is an Ordered number and an Ordered square if it is a square – Hexed Feb 01 '17 at 14:13
  • then wait the 90 minutes and write a full question about it with your code shown and so on. that will make it much easier to help you – XtremeBaumer Feb 01 '17 at 14:19
1

Something like this. Not perfect/optimized but should do the job.

    final int min = 0;
    final int max = 9;
    final String inputString= "testingThisString";
    String str = "";
    final Random random = new Random();
    for(final char c: inputString.toCharArray()){
        final int randomNumber = random.nextInt(max - min) + min;
        str += c + "" + String.valueOf(randomNumber);
    }
    System.out.println(str);
JonyD
  • 1,237
  • 3
  • 21
  • 34
1

A Stream might be useful in this case:

final Random rand = new Random();
String str = "string";
str = Stream.of(str.split("")).map(
             x -> x + Integer.valueOf(rand.nextInt(10))
      ).collect(Collectors.joining());
System.out.println(str); // s8t9r6i0n1g6

We get a stream of individual letters in the string, and add a random number to each string. Finally we join everything together to get a final string.

A small update. Since java-8 String class has chars methods which returns a stream of character. So we can use it directly without splitting the original string. For example:

Random rand = new Random();
String str = "string";
str = str.chars().mapToObj(
          c -> String.valueOf((char)c) + rand.nextInt(10)
      ).collect(Collectors.joining());
System.out.println(str); // s5t7r8i4n3g1 
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53