-5

Issue: I've completed steps 1-4 of this assignment. However, I'm currently stuck on steps 5 and 6 of this assignment, so I'm at a loss on how to combine my fizz and buzz String arrays into a separate fizzbuzz String array.

TL;DR I don't know how to do steps five and six.

Assignment:

You can do this all in the main method. This is using a game called Fizz-Buzz, an ancient programmer’s game.

  1. Start by initializing some variables to set the maximum and minimum value of a random number and for the capacity of an array. (20/100)

  2. Initialize three new arrays, one for a list of random numbers (as integers) and two for String arrays called ‘fizz’ and ‘buzz’.(20/100)

  3. You’ll also need an integer for counting.

  4. Write a for loop that generates a random number for each position in the array. Remember that the range for this will be set by the two variables initialized at the beginning of the file. There are multiple ways to create a random number, just find one that works for you. (20/100)

  5. Using the count of the arrays, create another array that will store all of the fizzes and buzzes without any extra space leftover in the array. (20/100)

  6. Use a for each loop to iterate the array and print all of the fizzes and buzzes, with no other output. (20/100)

What I've accomplished thus far:

            /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.Random;

/**
 *
 * @author
 */
public class FizzBuzz {

  //2a. Initialize one int array for a list of random numbers.
  private static int[] anArray;
  private static final int size = 10;

  //2b. Initialize two String arrays called 'fizz' and 'buzz.'
  public static String[] fizz;
  public static String[] buzz;
  public static String[] fizzbuzz;
  public static Random rand = new Random();

  //1. Set the maximum and minimum value of a random number.
  private static final int min = 0;
  private static final int max = 5;
  private static int count = 0;

  public static int[] list() {

    anArray = new int[size];
    //3. Make an integer for counting("counter" in the for loop)
    //4. Write a for loop that generates a random number for
    //   each position in the array.
    for(count = 0; count < anArray.length; count++) {
      anArray[count] = randomFill();
    }
    return anArray;
  }

  public static void print() {

    for (int i = 0; i < anArray.length; i++) {
      System.out.println(anArray[i] + ": " + fizz[i] + buzz[i]);
    }
  }

  public static int randomFill() {  
    return rand.nextInt((max - min) + 1) + min;
  }

  public static String[] getF() {

    fizz = new String[size];

    int x = 0;
    int counter;

    for(counter = 0; counter < fizz.length; counter++) {
      if(anArray[counter] % 3 == 0) {
      fizz[counter] = "fizz";
    } else {
        fizz[counter] = "";
      }
  }
    return fizz; 
  }
  public static String[] getB() {
    buzz = new String[size];

    int x = 0;
    int counter;

    for(counter = 0; counter < buzz.length; counter++) {

      if(anArray[counter] % 5 == 0) {
      buzz[counter] = "buzz";
    } else {
        buzz[counter] = "";
      }
  } 
    return buzz;
  }

  public static String[] getFB() {
    fizzbuzz = new String[size];


    return fizzbuzz;
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    list();
    getF();
    getB();
    print();
  } 
}
moot
  • 3
  • 3
  • you never call `GetFizzOrBuzz()`. So you never assign any values to `fizzbuzz`. This code won't work at all, since `fizzbuzz` is never initialized. You can/should try to reproduce this result with a debugger (there should be one built in to whatever IDE you're using). –  Jun 27 '16 at 01:51
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –  Jun 27 '16 at 01:51
  • @Paul Hey, thanks for the input. I realized that immediately after I posted the question, and edited the post to reflect me calling GetFizzOrBuzz()! – moot Jun 27 '16 at 01:53

1 Answers1

0

First of all:

How Random works

Your implementation of Random won't quite work for simple reason:
If you call the default-constructor, the seed of the PRNG will be the current time, thus you're quite likely starting multiple PRNGs with the same seed and thus receive the same random-value multiple times. Use a single Random-instance instead:

private Random rnd = new Random();

public static int randomFill() {
    return rnd.nextInt((max - min) + 1) + min;
}

This should help in understanding the issue better.

General implementation

Try to stick as close to the given task as possible. E.g. the problem explicitly states that you should use a variable to specify the length of the arrays, while you're using a fixed magic-number. The task explicitly states to use two arrays fizz and buzz, you're using one array called fizzbuzz.

Inside GetFizzBuff, you probably meant x = 2; instead of x = x + 2;. Apart from that this part is fine.

Output

As above: try to stick to the given task. It's explicitly stated to only output the fizzes and buzzes, not any integers. Or at least try to print a value and the corresponding fizzes and buzzes in the same line, to produce readable output.