0

Does anyone know how I can randomly generate an input string into a picture on the horizontal and vertical axis?

    public static String change(Graphics B) {
    Scanner termIn = new Scanner(System.in);
    System.out.print("User, please give me a string to print:\n\t");
    String in = termIn.nextLine();

    String s1 = in;
    String civic = s1.replace("car", "karr").replace("cool","crazy")
    .replace("the","teh").replace("red","blue").replace("kouki","cookie")
    .replace("nissan", "datsun").replace("s14","silvia").replace("drift","slide")
    .replace("laugh","LOL").replace("240sx", "sr20").replace("drive","shift");

    System.out.println(civic);
    B.drawString(civic, 60, 259);
    return s1;
  • 1
    We're going to need to see what you've already written before we can provide any help. – Mike G Apr 04 '14 at 17:47
  • i want to randomly generate an input string onto a picture but am not sure what code to use, sorry this is my first time – user3357247 Apr 04 '14 at 17:50
  • 2
    Possible duplicate: http://stackoverflow.com/questions/2658554/using-graphics2d-to-overlay-text-on-a-bufferedimage-and-return-a-bufferedimage/2658663#2658663 – gla3dr Apr 04 '14 at 17:52
  • it seems to be only for "given " points, i want it to generate itself at random points in the picture – user3357247 Apr 04 '14 at 17:55
  • http://stackoverflow.com/q/363681/1015495 – Mike G Apr 04 '14 at 17:56

1 Answers1

0

In java, you randomly generate numbers, which you could then use to select strings or words from a dictionary.

class RandomizeString {
    private String[] pronouns = new String[] {"he", "I", "We"};
    private String[] verbs = new String[]{"ate", "want", "drove"};
    private String[] cars = new String{"datsun", "toyota", "subaru"};

    public String randPronoun() { return pronouns[Math.random() * pronouns.length]; }
    public String randVerb() { return verbs[Math.random() * verbs.length]; }
    public String randCar() { return cars[Math.random() * cars.length]; }

    public String randomize(String in) {
       return in.replace("he", randPronoun()).replace("she", randomPronoun())
                .replace("flew", randomVerb()).replace("smashed",randomVerb())
                .replace("ford",randomCar()).replace("chevy",randomCar());
    }
}

Now you have a class whose only job is randomizing a string. It stores words in private arrays, it has a set of methods that select random words from those arrays, and it has a method that uses those methods to do the replacement in a string parameter.

If you wanted, you could add new arrays for whatever (animals, names, countries, colors, etc), along with methods to fetch a random value and you could also do stuff like

 `in.replace(randomCar(), randomAnimal()).replace(randomVegie(), randomIceCream())`
deanosaur
  • 621
  • 3
  • 5
  • thank you, just a quick question though, would i put the randomly generate code into my method or put it into main when i call my method? – user3357247 Apr 04 '14 at 18:34