I have to make a text file in which I'm supposed to put 100 random characters where on each even position should be a small letter and on each odd position should be a capital letter. Also, all the letters are supposed to be separated by a blank field (space). I only succeeded to make a 100 random characters list but I don't know how to do the rest.
public class Main {
public static char getRandomCharacter(char c1, char c2) {
return (char) (c1 + Math.random() * (c2 - c1 + 1));
}
public static char[] createArray() {
char[] character = new char[100];
for (int i = 0; i < character.length; i++) {
character[i] = getRandomCharacter('a', 'z');
}
for (int i = 0; i < character.length; i++) {
System.out.println(character[i]);
}
return character;
}
public static void main(String[] args) {
createArray();
}
}
I would appreciate the help.
EDIT: Here's an edited version. However, the output is not much different b j r m r b k i...
public class Main {
public static char getRandomCharacter(char c1, char c2) {
return (char) (c1 + Math.random() * (c2 - c1 + 1));
}
public static char[] createArray() {
char[] character = new char[100];
for (int i = 0; i < character.length; i++) {
character[i] = getRandomCharacter('a', 'z');
}
for (int i = 0; i < character.length; i++) {
System.out.println(character[i]);
}
return character;
}
public static void main(String[] args) {
char[] arr = createArray();
StringBuilder text = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
int pos = i + 1;
if (pos % 2 != 0) {
String s = "" + arr[i];
text.append(s.toUpperCase());
} else {
text.append(arr[i] + " ");
}
}
String content = text.toString().trim();
}
}