1

How do I instantiate my array so it has 26 random characters in it?

import java.util.Random; 

public class Encryption {

Random r = new Random();
char[] real = {'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'};
char[] decrypted = //??????

for( i=0; i < decrypted.length; i++) {

}
PatGreens
  • 129
  • 2
  • 10
  • 1
    To instantiate an array either you need to know the size of the array or the contents of the array. `char[] decrypted = new char[26];` – RP- Jan 30 '14 at 21:49
  • is there a more conventional way of putting every letter in the alphabet in my array than what I did? – PatGreens Jan 30 '14 at 21:50
  • 1
    Do you need shuffled array? Or it is possible to have duplicates? – ferrerverck Jan 30 '14 at 21:51
  • I am doing introductions to java so I am not sure what those terms mean. – PatGreens Jan 30 '14 at 21:52
  • so what syntax do I put in my for loop to get random characterS? – PatGreens Jan 30 '14 at 21:53
  • 1
    you could have a `for` loop, like `char[] real = new char[26]; for(i = 97; i <= 122 ; i++) { real[i-97] = (char)i; }`. ASCII values of a-z are 97 to 122 – RP- Jan 30 '14 at 21:55

2 Answers2

2

Instantiate the array of size 26;

char[] array = new char[26];

Add random chars (within the range of lowercase letters)

for(int i = 0; i < array.length; i++){
     array[i] = (char)(r.nextInt(26) +97);
     System.out.print(array[i]);
}

You can modify this to create your first array by removing the random part and using i

for(int i = 0; i < array.length; i++){
     array[i] = (char)(i + 97);
     System.out.print(array[i]);
}
Java Devil
  • 10,629
  • 7
  • 33
  • 48
  • why +97? I dont understand that part – PatGreens Jan 30 '14 at 21:56
  • 1
    @PatGreens 97 is the decimal value of the character `a`. Look at this [ASCII character chart](http://www.asciitable.com/). So I add this so that the minimum int possible is 97 ... which is `a` when cast as a char – Java Devil Jan 30 '14 at 21:57
  • ooooh I see thats interesting. SOrry im fairly new to this – PatGreens Jan 30 '14 at 21:58
  • @PatGreens Everyone needs to start somewhere :). If you wanted uppercase Chars you add 65 – Java Devil Jan 30 '14 at 22:00
  • What if the integer yields something like a DEL or a ~ though, is there a way to keep it where the char ASCII ends at 122? – PatGreens Jan 30 '14 at 22:01
  • @PatGreens In my examples, the call `r.nextInt(26)` is what controls this. The maximum number the `nextInt()` method will return is 25. Then adding 97 yeilds 122. No higher value is possible. In the second code block, it is the array length which controls this. Provided the array is no longer than 26 it won't go past 122 – Java Devil Jan 30 '14 at 22:38
0

To fill in your example:

import java.util.Random; 

public class Encryption {

  Random r = new Random();
  char[] real = {'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'};
  char[] decrypted = new char[26];

  public static void main(String[] args){
    for( i=0; i < decrypted.length; i++) {
      decrypted[i] = real[r.nextInt(26)];
    }
  }
}
Danny A
  • 13
  • 2
  • does this return a character – PatGreens Jan 30 '14 at 22:10
  • Does what return a character? – Danny A Jan 30 '14 at 22:19
  • On second look, see how you call the array decrypted, I think I misunderstood what you require. What is decrypted supposed to be? Is it the array you need to instantiate with 26 random characters in it? Presumably later you will be encrypting it with some basic shift cipher? – Danny A Jan 30 '14 at 22:23