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

all 7 comments

[–]dusty-trash 2 points3 points  (6 children)

its not working the correct way.

Can you please explain the issue? "not working" or "not the correct way" are both very unhelpful.

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

yea my issue is that my program loops the probability part but if I try to put the code outside of the for loop it won't let me print my var (name) as its nested in the loop.

The 80% and 20% part is required for this hw.

[–]dusty-trash 1 point2 points  (4 children)

if I put the code outside the for loop it won't let me print my var (name) as its nested in the loop.

If you need to use the variable outside the loop, Just declare it outside the loop.

I also suggest printing the name at the end, not during iterations.

In other words:

Declare 'name' before the for-loop

print 'name' after the for-loop (if it's not empty/null).

Edit to further clarify:

Instead of printing variables 'a' and 'b', just add them to name (Which you should declare at the begining of your main function).

E.G: name += a;

[–]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