Home > Mobile >  Random characters java
Random characters java

Time:05-29

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();

    }

}

CodePudding user response:

In your case it's easier to create a string first and then put it into a txt file.

You can simply iterate through the random array and check for odd/even positions. Use Character.toUpperCase or Character.toLowercase in respective odd even postion and append to the string.

Use StringBuilder so that you don't have to create new String objects every time.

char[] arr = createArray();
StringBuilder text = new StringBuilder();
//iterate thoruh the arr
for (int i = 0; i < arr.length; i  ) {
    int pos = i 1;
    if(pos%2!=0){  // check whether current postion is odd or even                 
        text.append(Character.toUpperCase(arr[i]) " ");// if position is odd then convert it to uppercase
    }else{
        text.append(arr[i] " ");  // if position is even then convert it toLowercase if needed (if arr contents both upper case and lower case)
    }
}
String content = text.toString().trim(); // trimin to remove any extra space in last or first of the string.

To write in file

String path = "D:/a.txt";
Files.write( Paths.get(path), content.getBytes());

EDIT 1 : I am not sure why this Character.toUpperCase is not working. You can try this below conversion, the drawback is that a String object will be created for every even position.

String s = ""  arr[i];
text.append(s.toUpperCase() " ");

EDIT 2: You can use normal String

String text = "";
for (int i = 0; i < arr.length; i  ) {
    int pos = i   1;
    if (pos % 2 != 0) {
        String s = ""   arr[i];
        text =text   s.toUpperCase() " ";
    } else {
        text= text   (arr[i]   " ");
    }
}
String content = text.toString().trim();
System.out.println(content);
  • Related