I have a program and I need it to ask the user for a word eg hello and then ask for a pattern eg 2 and then take that word and split up the characters and put random letters between the letters of the word so in this case the users pattern was 3
so hello
would be outputted as **h**as**e**rg**l**ty**l**oh**o**
. So I have the part where I ask the user for a word and I spilt up the characters of the word with System.out.print(userWord.charAt(0));
I just need to know how you would generate random different letters each time between the words characters . The section of my code that generates the letters is this:
public static void main (String[] args)
{
int cipherchoice = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a number!"));
int z;
String mixedarray1 [] = { "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",};
int x = mixedarray1.length; // determines the length of mixedarray
String[] myarray = new String [cipherchoice]; // create a new arracy to be coded
for (int i = 0; i < cipherchoice; i++) // loop equal size of coded array
{
z = (int)(Math.random()*x); // to create a random index value from mixedarray
myarray[i] = mixedarray1[z]; // assigned myarray a random index value from mixedarray
System.out.print (myarray [i]); // prints results using print (not println)
}
}
So my output is System.out.print(myarray [i]);
So how do you get multiple different outputs (Eg if the user enters 5 in another section of code how would you get 5 different outputs of random letters?
Put your code into a method and call it n
times. Eg
void printRandomLetter() {
... the code you posted ...
}
...
void someOtherMethod() {
for (int i = 0; i < userInput; i++) {
printRandomLetter();
}
}
Use the random.org service via HTTP , by issuing an HTTP GET request to https://www.random.org/strings/?num=10&len=8&digits=on&upperalpha=on&loweralpha=on&unique=on&format=html&rnd=new , in Java as follows:
URL url = new URL("https://www.random.org/strings/?num=10&len=8&digits=on&upperalpha=on&loweralpha=on&unique=on&format=html&rnd=new");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
Your strings will be in result. Exception handling is left as an exercise to the reader.
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.