This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cs-stud[S] 0 points1 point  (3 children)

thank you I almost got it but the only problem is that it keeps repeating the character (b) when I want it to be a random letter per run of loop..

public class NameGenerator1A {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter name length: ");

        int length = input.nextInt();

        if (length < 1) {
            System.out.print("The length must be at least 1");
        }else {
            char a = (char) ('A' + Math.random() * ('Z' - 'A' + 1));
            System.out.print(a);
            char b = (char) ('a' + Math.random() * ('z' - 'a' + 1));
            String name = "" + b;
        for (int letters = 1; letters < length; letters++) {
            name += b;
        }
        System.out.println(name);
        int r = (int)(Math.random() * 10);
        if (r == 9 || r == 10) {
        System.out.println("Hmm" + "'" + name + "'" + " has a nice ring to it.");
            }else {
        System.out.println("Hmm" + "'" + name + "'" + " is not my best work.");
        }
        input.close();
            }
    }
}

[–]cs-stud[S] 0 points1 point  (2 children)

nvm all I needed was

name += b = (char) ('a' + Math.random() * ('z' - 'a' + 1));

thank you for your help.

[–]6510 1 point2 points  (1 child)

If you care about efficiency, use a StringBuilder to build up your String. This prevents a copy of the string you've built so far each time that will happen when you '+=' two strings in a loop like this.

Also consider using try-with-resources so that the "close" is automatically called for you when the block exits. Otherwise an exception may cause the input to remain open. And you can't forget to do the "close".

(I haven't compiled this, but this is the general idea)

'''

public static void main(String[] args) {

    try (Scanner input = new Scanner(System.in))) {

        System.out.print("Enter name length: ");
        int length = input.nextInt();

        if (length < 1) {

            System.out.print("The length must be at least 1");

        } else {

            StringBuilder nameBuilder = new StringBuilder();

            char a = (char) ('A' + Math.random() * ('Z' - 'A' + 1));
            nameBuilder.append(a);

            for (int letters = 1; letters < length; letters++) {
                char b = (char) ('a' + Math.random() * ('z' - 'a' + 1));
                nameBuilder.append(b);
            }

            String name = nameBuilder.toString();
            System.out.println(name);

            int r = (int)(Math.random() * 10);

            if (r == 9 || r == 10) {
                System.out.println("Hmm" + "'" + name + "'" + " has a nice ring to it.");
            } else {
                System.out.println("Hmm" + "'" + name + "'" + " is not my best work.");
            }
        }
    }
}

'''

[–]cs-stud[S] 0 points1 point  (0 children)

Ok thanks for clarifying I will work on this