-3

I was taking a shot at the FizzBuzz problem and decided to use an array to store the first 15 results in array and then iterate through it. But the variable stored in array is not updating its value if updated later in the loop

import java.util.Scanner;

 public class FizzBuzz {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter the number"); 
    int Num= sc.nextInt();
    String F= "Fizz";
    String B= "Buzz";
    String FB= "FizzBuzz";
    String I="";
    String arr[]= {FB, I, I, F, I, B, I, I, I, F, B, I, F, I, I};
    for (int j = 1; j <= Num; j++) {
        I = Integer.toString(j);
        System.out.println(arr[j%15]);
    }
  }
}

The variable I does not change its value in the for-loop. It just prints empty spaces in the result for the I variable. HeLp !

P.S: Is this a good implementation with respect to naive soln?

  • Reassigning something to `I` does not change the previous uses of `I`. Apart from that: no idea how that code is supposed to fizzbuzz in the first place. Why do you start of with *knowing* which value is fizz, buzz or fizzbuzz!? That is nonesense. – luk2302 Feb 24 '21 at 09:12
  • Reassigning the variable `I` does not affect elements in the array. They are separate variables. – khelwood Feb 24 '21 at 09:12
  • 2
    BTW, element 6 should be fizz presumably. – khelwood Feb 24 '21 at 09:13
  • 1
    No, this is a terrible solution, it does not work, it assume prior knowledge of fizzbuzz outcome, the variables are mis-named, it has invalid comments in it, ... It is similar to `public int getRandomNumber() { return 3; /* randomly choosen */ }` – luk2302 Feb 24 '21 at 09:18
  • @luk2302 to be fair, the FizzBuzz sequence is periodic with period 15, so you absolutely do know what output comes next. – QBrute Feb 24 '21 at 09:27
  • @luk2302 [It should be `return 4`](https://xkcd.com/221/) ;-) – MC Emperor Feb 24 '21 at 09:31

2 Answers2

0

The reason why the variable does not update is the following: If you create a local variable, like here the String I = ..., and pass it to an array, it will be stored there as the instance of the variable. If you change a string (or value of a string), the instance changes. So a string is considered as an instance. If you create a new string, you automatically create a new instance - (String s =) new String(...) is equivalent to changing the variable s = "...". You could write a wrapper for the string, where a string value is "mutable", i.e. changeable, without changing the actual instance passed in the array.
You can also change your code, without the need of a wrapper:

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number");
    int num = sc.nextInt();
    String f = "Fizz";
    String b = "Buzz";
    String fb = "FizzBuzz";
    String i = "";
    String[] arr = {fb, i, i, f, i, b, i, i, i, f, b, i, f, i, i};
    for (int j = 1; j <= num; j++) {
        int index = j % 15;

        //local constant to check for equality to the i-variable
        final String pos = arr[index];

        String strToInt = Integer.toString(j);
        
        //Check for equality to the "i"-variable
        if (pos.equals(i)) {
            arr[index] = strToInt;
        }

        //Changing the i-instance to the strToInt instance
        i = strToInt;
        
        //Printing your result
        System.out.println(arr[index]);
    }
}

I also changed the variable names to valid ones, as the JNC (Java Naming Conventions) describe.

Yours sincerly,
Vinzent

mindcubr
  • 3
  • 1
-1
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number");
        int num = sc.nextInt();
        String f = "Fizz";
        String b = "Buzz";
        String fb = "FizzBuzz";
        String i = "";
        String[] arr = {fb, i, i, f, i, b, i, i, i, f, b, i, f, i, i};

        String indexValue;
        for (int j = 1; j <= num; j++) {
            indexValue = arr[j % 15];
            System.out.println(indexValue.equals(i) ? j : indexValue);
        }
    }
}
Roie Shlosberg
  • 189
  • 1
  • 7