-2

I have made 3 random numbers made strings from them. I don't know why my colors are outputting nullnullnull, instead of B,R,or G in any order.Any help is appreciated.I am only allowed to use methods, random numbers, if statements, and strings. MY CODE:

 public static void main(String[] args) {
        // TODO code application logic here

    randomWholeNumber(colour,colour2,colour3);
   newGame(colour,colour2,colour3);
    }
    static void randomWholeNumber(String colour,String colour2,String colour3){

        int randnum=(int)(Math.random()*3);
        if(randnum==0){

        }
        else if(randnum==1){

        }
        else if(randnum==2){ 


        int randnum2=(int)(Math.random()*3);
        if(randnum2==0){

        }
        else if(randnum2==1){

        }
        else if(randnum2==2){ 


        int randnum3=(int)(Math.random()*3);
        if(randnum3==0){

        }
        else if(randnum3==1){

        }
        else if(randnum3==2){ 

    }
        }
        }
    }

    static void newGame(String colour,String colour2,String colour3){
        System.out.println(colour+colour2+colour3);
    }
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304

3 Answers3

2

Java is pass by value. That means the values you have passed in can be edited but not replaced. As String cannot be edited in Java (immutable), you have a problem.

randomWholeNumber(String colour, String colour2, String colour3)

colour1, colour2 & colour3 are your values. What is passed into the method is a copy of the value (address) of these object in memory, if you set them to a new object, with colour="B"; for example, a new object is created and the values you are working with in your method are set to the value (address) of the new object in memory. This means the original object reference is lost inside the method and you are working on a new object.

This then means when you return from the method you are back with the original references with no changes as the ones you changed where the new ones you created and they were lost when you exited the method.

Steps to get your required behaviour

In order to be able to get it working you will either need to create your own MyColour class which contains three Strings. Pass that into the method and set it's three Strings in there. This way the address of the object you passed in is not lost and you can make changes to it, as long as you don't create and replace it with a new one inside the method.

The other option is to return the 'R', 'G' or 'B' from the method and assign it to the strings by calling the method.

Community
  • 1
  • 1
Ross Drew
  • 8,163
  • 2
  • 41
  • 53
1

In java, parameters can be read from (passed by value), but not written to. Use the function's result to return something.

See this answer for an example

Community
  • 1
  • 1
dst
  • 3,307
  • 1
  • 20
  • 27
0

In Java - unlike other languages like C++ - the values of your colours are passed into your method, not the references. That is why the colours are still null when printing them in your code; the original variables are not touched.

You should rewrite your method so that it returns the new values. This would be compatible with your code:

String[] colours = randomWholeNumber();
System.out.println(colours[0] + colours[1] + colours[2]);
}
static String[] randomWholeNumber(){

    String colour, colour2, colour3 = null;

    // Same code like before

    // Put the colours into a string array
    return { colour, colour2, colour3 };
}
andreas
  • 7,844
  • 9
  • 51
  • 72