0

Are there any resources available in the java documentation, or elsewhere that can lead me in the right direction on this? Even just giving the method(s) would help greatly.

Let's say I prompt a user to enter an integer i greater than 0. Then, in the output, they get a string that is the length of the integer. The characters can range from "!" to "~" on the ASCII table (33-126).

For example:

"Enter an integer greater than 0: " Input: 8 Output: &3lR(c$2 "Enter an integer greater than 0: " Input: 4 Output: I*@f Etc, etc...

I think I can figure out the rest myself, such as generating a random (a real random, not a pseudo-random), doing the necessary loop to print an error message if the input is less than or equal to 0. I would prefer method hints over code anyway. Thank you.

DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52

3 Answers3

1

Use a loop with StringBuilder:

String randomString(int lengthOfString){
    int minChar = 33;
    int maxChar = 126;

    StringBuilder result = new StringBuilder();
    for (int i = 0; i < lengthOfString; i++){
        result.append((char) generateRandomBetweenTwoNumbers(minChar, maxChar));
    }
    return result.toString();
}
Jorah Mormont
  • 126
  • 1
  • 7
0

Once you use your random number generator you can just construct the String by converting the int's to char's:

String output = (char)40 + "" + (char)60....
Gregory Basior
  • 300
  • 1
  • 9
0

Here's a way to do it using code a beginner would have seen; I don't recommend this.

import java.util.Random;

public class stack {

    public static void main(String[] args) {

        int length = 8;

        char character = 0;

        String output = "" + character;

        Random random = new Random();

        for (int i = 0; i < length; i++) {

            character = (char) (random.nextInt(126 - 33 + 1) + 33);

            output += character;

        }

        System.out.println(output);

    }

}
Jack
  • 497
  • 3
  • 10
  • 22
  • That looks pretty simple, it's just that the string must be created from a number that a users inputs. If the user puts in 8, the the string must be 8 characters long. If the user puts in 20, then the string must be 20 characters long. – DevOpsSauce Feb 22 '15 at 19:34
  • And it was recommended that we not use Math.random, since it's considered "pseudo-random." The funny part is that we haven't learned anything else. SecureRandom was briefly mentioned, but it went no further than saying "this is more secure than Math.random()." Some of these assignments are difficult since we are write programs only based on what we have learned. – DevOpsSauce Feb 22 '15 at 19:37
  • @csheridan So set the length variable to whatever the user inputs? Read this to learn the difference between random and SecureRandom http://stackoverflow.com/questions/11051205/difference-between-java-util-random-and-java-security-securerandom – Jack Feb 22 '15 at 19:49
  • Yes @Jack. The randomly generated string length will be equal to the integer the user inputs. Also, I discovered that I am allowed to use Math.random. – DevOpsSauce Feb 23 '15 at 03:22
  • Also, @Jack, can you explain what is going on in this section? character = (char) (33 + (int) (Math.random() * ((126 - 33) + 1))); – DevOpsSauce Feb 23 '15 at 03:24
  • When I run it, it starts from the first character, goes to the next line, prints the first and second, and so on until it reaches the length specified by the integer. For example, if I input 5, it outputs (I'll separate with spaces to signify a new line): * *& *&U *&U% *&U%2 – DevOpsSauce Feb 23 '15 at 03:34
  • @csheridan I updated the code to generate a number using the Random class. It's better explained here. http://stackoverflow.com/a/363692/3877799 As for setting the length to a number that the user inputs, try searching on this site how to use the Scanner class. – Jack Feb 23 '15 at 07:49