Question

In: Computer Science

I cannot figure out why my random name generator isn't producing the letter u or z....

I cannot figure out why my random name generator isn't producing the letter u or z. Can someone help me out? The language is java.

import java.util.Random;

public class BrandName {

   public static void main(String[] args) {
       Random random = new Random();
       System.out.println("Brand Name Generator - Michael Wikstrom\n");
       char odd[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y',
               'z' };
       char even[] = { 'a', 'e', 'i', 'o', 'u' };

       for (int i = 1; i <= 10; i++) {

           int length = random.nextInt(4) + 8;
           String name = " ";
           for (int j = 0; j < length; j++) {
               if (j == 0) {
                   int pos = random.nextInt(odd.length - 1);
                   name += Character.toUpperCase(odd[pos]);

               } else if (j % 2 == 1) {
                   int pos = random.nextInt(even.length - 1);
                   name += even[pos];
               } else {
                   int pos = random.nextInt(odd.length - 1);
                   name += odd[pos];
               }
           }

           System.out.println(i + ") " + name);

       }
   }

}

Solutions

Expert Solution

Please find your solution below and do upvote if doubt comment.

  • The problems are due to : random.nextInt(n)
  • This will return a rundom number from 0(inclusive) to n(exclusive) i.e only 0 to n-1 number will be retuned.
  • Same in the given code random.nextInt(odd.length - 1) the odd array length is 20 and then the function will be random.nextint(19) i.e it will return number from 0 to 18 only so the last index position which is 19(position of z) is excluded that's why z is not being produced.
  • Same with the random.nextInt(even.length-1) the length of even array is 5 so the function be random.nextInt(4) i.e it will generate number from 0 to 3 only so the last index position which is 4(position of u) is excluded that's why u is not being produced.

Corrected code:

import java.util.Random;

public class BrandName {

   public static void main(String[] args) {
       Random random = new Random();
       System.out.println("Brand Name Generator - Michael Wikstrom\n");
       char odd[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y','z' };
       char even[] = { 'a', 'e', 'i', 'o', 'u' };

       for (int i = 1; i <= 10; i++) {

           int length = random.nextInt(4) + 8;
           String name = " ";
           for (int j = 0; j < length; j++) {
               if (j == 0) {
                   int pos = random.nextInt(odd.length);
                   name += Character.toUpperCase(odd[pos]);

               } else if (j % 2 == 1) {
                   int pos = random.nextInt(even.length);
                   name += even[pos];
               } else {
                   int pos = random.nextInt(odd.length);
                   name += odd[pos];
               }
           }

           System.out.println(i + ") " + name);

       }
   }

}


Related Solutions

For Python, I cannot figure out why "and" is printing at the beginning of my list....
For Python, I cannot figure out why "and" is printing at the beginning of my list. Can someone help? listToPrint = [] while True: newWord = input("Enter a word to add to the list (prest return to stop adding words) > ") newWord == "": break else: listToPrint.append(newWord) for item in listToPrint[:-1]: print(item, end=', ') print('and', listToPrint[-1]) If I enter m, n, b to the list, it executes to and m, n, b
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user decide the size of a 2d array then fill said array. #include <iostream> using namespace std; typedef int* IntArrayPtr; int main(void) { int column, row, i, j; IntArrayPtr *arr_num = new IntArrayPtr[row]; cout << " " << endl << " This program creates a 2D array of dynamic memory, fills it with user entered numbers, " << endl << " Then prints the array...
Cant figure out why my out is wrong for my c++ program. for example if the...
Cant figure out why my out is wrong for my c++ program. for example if the initial value is 100, the intrest rate is 10%, and it takes 6 months for maturity the output should be $105 but instead it outputs 101.657 please help. #include <iostream> #include <cmath> using namespace std; struct account { double balance; double interest_rate; int term; }; void info(account& accountinfo); int main(void) { double calc1, calc2, calc3; account accountinfo; info(accountinfo); calc1 = accountinfo.interest_rate / 100; calc2...
in java: In my code at have my last two methods that I cannot exactly figure...
in java: In my code at have my last two methods that I cannot exactly figure out how to execute. I have too convert a Roman number to its decimal form for the case 3 switch. The two methods I cannot figure out are the public static int valueOf(int numeral) and public static int convertRomanNumber(int total, int length, String numeral). This is what my code looks like so far: public static void main(String[] args) { // TODO Auto-generated method stub...
Show work please, this is the last problem for my homework and I cannot figure this...
Show work please, this is the last problem for my homework and I cannot figure this out for the life of me An IP4 datagram arrived with the following information in the header (in hexadecimal):                0x4A 00 00 56 00 03 58 50 18 06 58 50 7C 4E 03 02 B4 0E OF 15 2f………. Are there any options? How many bytes, if any? How many more routers can the packet travel to? Explain. What is the size...
I have done the first part of this problem, but I cannot figure out the second...
I have done the first part of this problem, but I cannot figure out the second part. I know there are several different answers already posted on Chegg for this particular problem with different acceleration and time, but I need this one specifically, with a step by step solution. Again...just PART B....the answer to Part A is 201.72 A helicopter carrying Dr. Evil takes off with a constant upward acceleration of 4.20 m/s2. Secret agent Austin Powers jumps on just...
this is my assignment for intro to java tha i could not figure out, my system...
this is my assignment for intro to java tha i could not figure out, my system just crashed wit a lot of errors. thank for your help Create a new Java class inside your project folder. The name of the class should be: TempConverter Note that this means you should have the following line at the top of your program: public class TempConverter Write a program that allows the user to convert a temperature given in degrees Celsius or Fahrenheit...
I cannot for the life of me figure out the definitions of the correspondence, coherence, pragmatic...
I cannot for the life of me figure out the definitions of the correspondence, coherence, pragmatic and semantic theories of truth. I've tried deciphering the definitions from my textbook but I still can't wrap my head around it. I need someone to dumb it down for me. Thank you in advance.
I keep getting an error that I cannot figure out with the below VS2019 windows forms...
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue string id; while (id = sr.ReadLine() != null) using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Dropbox13 { public partial class SearchForm : Form { private List allStudent = new List(); public SearchForm() { InitializeComponent(); } private void SearchForm_Load(object...
I cannot figure out this problem, I've made a few attempts at solving it. I could...
I cannot figure out this problem, I've made a few attempts at solving it. I could really use some help. The question is : 75.00 mL of 0.225 M HNO2 is titrated to its equivalence point with 0.100 M NaOH. What is the pH at its' equivalence point? Please help!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT