Home > Net >  Unwanted null value when iterating through Java array
Unwanted null value when iterating through Java array

Time:04-19

I am iterating through an array 99 times to display an output for the folk song ""99 Bottles". There is a null value that is attaching itself to the return output - why is this? Answered and updated code below

*edited The console stops at 50 bottles of beer. This is because of the line (beercounter-1) " bottles of beer on the wall. \n\n"; How can I get around this? Answered and updated code below

public class Bottles {

    private int beercounter = 99;
    String result = "";
    String[] append = new String[99];

    public String BottleSong() {

        for (int i = 0; i < append.length; i  ) {
            
            
            result  = beercounter   " bottles of beer on the wall"   
            "\n"   beercounter   " bottles of beer"   
            "\nTake one down, pass it around,"   "\n"  
            (beercounter-1)   " bottles of beer on the wall. \n\n";
            beercounter--;

            result  = append[i];
        }
        return result;

    }
    
    

    public static void main(String[] args) {
        
        Bottles bottle = new Bottles();
        
        System.out.println(bottle.BottleSong());


    }

}

Output:

99 bottles of beer on the wall
99 bottles of beer
Take one down, pass it around,
98 bottles of beer on the wall. 

null98 bottles of beer on the wall
98 bottles of beer
Take one down, pass it around,
97 bottles of beer on the wall. 

etc
etc

CodePudding user response:

You declare the append[] string array but you never set any values there. Hence the following line:

result  = append[i];

will always append a null value to the string. Actually, the song narrative is being concatenated together and stored in the result string, and your code doesn't really use append[]. Therefore, you can probably just drop the latter array entirely from your code.

CodePudding user response:

As Tim pointed out, I wasn't using the append[] String array up top for any practical purpose. I changed the code to remove this array and also the line

result  = append[i];

I then changed the interation parameter to be less than beercounter

for (int i = 0; i < beercounter; i  ) {

Updated code below

public class Bottles {

    private int beercounter = 99;
    String result = "";

    public String BottleSong() {

        for (int i = 0; i < beercounter; i  ) {
            
            
            result  = beercounter   " bottles of beer on the wall"   
            "\n"   beercounter   " bottles of beer"   
            "\nTake one down, pass it around,"   "\n"  
            (beercounter-1)   " bottles of beer on the wall. \n\n";
            beercounter--;

        }
        return result;

    }
    
    

    public static void main(String[] args) {
        
        Bottles bottle = new Bottles();
        
        System.out.println(bottle.BottleSong());


    }

}

And new output:

99 bottles of beer on the wall
99 bottles of beer
Take one down, pass it around,
98 bottles of beer on the wall. 

98 bottles of beer on the wall
98 bottles of beer
Take one down, pass it around,
97 bottles of beer on the wall. 

etc
etc
  • Related