简体   繁体   中英

Randomize user input based on questions

Im trying to collect some data from a user and display it with the user input. the example i was giving is:

Filename: output.txt
number of lines: 4
Line Length: 8
Character Set: ABC123

2CB3A32C
BB13CAA3
C3A21CB2
CC2B13A3

i currently have gotten the user input but i dont know how to display the random letters and numbers based on the input. Here is my code. Any help would be big. the data has to be displayed using Loop.

public static void main(String[] args) throws IOException
{
    int lineNum = 0;
    int numChars = 0;
    String charSet = "";
    String userInput = "";
    String filename;

    //Creates a Scanner Object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    //Get the filename.
    System.out.print("Enter a filename: ");
    filename = keyboard.nextLine();

    System.out.print("Enter number of lines: ");
    lineNum = keyboard.nextInt();

    if( lineNum < 1 || lineNum > 20){
        lineNum = 20;
        System.out.println("Defaulting to 20 lines");
    }

    System.out.print("Enter number of characters in each line: ");
    numChars = keyboard.nextInt();
    keyboard.nextLine();

    if( numChars < 1 || numChars > 20){
        numChars = 20;
        System.out.println("Defaulting to 20 characters");
    }

    System.out.print("Enter character set: ");
    charSet = keyboard.nextLine();

    //Put all the input together to display the results 

    PrintWriter pw = new PrintWriter(filename);
    pw.println("\nFilename: " + filename);
    pw.println("Number of lines: " + lineNum );
    pw.println("Line Length: " + numChars );
    pw.println("Character set: " + charSet );
    pw.println("\n" + userInput );
    pw.close();

    // Read the file
    BufferedReader br = new BufferedReader(new FileReader(filename));
    for (String line; (line = br.readLine()) != null;) {
        System.out.println(line);
    }

}

Try this.

Random random = new Random();
for (int i = 0; i < lineNum; ++i) {
    for (int j = 0; j < numChars; ++j)
        pw.print(charSet.charAt(random.nextInt(charSet.length())));
    pw.println();
}

Take a look at: RandomStringUtils

This might help get you on the right track: import org.apache.commons.lang.RandomStringUtils; System.out.println(RandomStringUtils.random(8,new char[]{'a','b','c','1', '2', '3'})); import org.apache.commons.lang.RandomStringUtils; System.out.println(RandomStringUtils.random(8,new char[]{'a','b','c','1', '2', '3'}));

尝试:

str.charAt(ThreadLocalRandom.current().nextInt(0, str.length()));

In Java8

final int length = 8;
final Random rand = new Random();
String random = IntStream.range(0, length).mapToObj(i -> str.charAt(rand.nextInt(100) % str.length()) + "").collect(Collectors.joining());
System.out.println(random);

random string generated for 10 runs

A31CCCB3
1AC3A2CA
BAB11B2A
A33A1ACA
BCCCB2AC
331C12CA
3CC1AAB3
113BAABB
1BC22B1A
31BBCAC1

You can use the below Utilty class

import java.util.Random;

public class RandomString {

  private char[] symbols;

  private final Random random = new Random();

  private final char[] buf;

  public RandomString(int length ,char[] symbols) {
    if (length < 1)
      throw new IllegalArgumentException("length < 1: " + length);
    buf = new char[length];
    this.symbols = symbols;
  }

  public String nextString() {
    for (int idx = 0; idx < buf.length; ++idx) 
      buf[idx] = symbols[random.nextInt(symbols.length)];
    return new String(buf);
  }
}

To Use it from your main,

RandomString randString = new RandomString(numChars ,charSet.toCharArray());
        for (int i = 0; i < lineNum; i++) {
            System.out.println("" +randString.nextString());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM