0
public static double[] processUserInput(String data) {
        String[] arrayInString;
        double[] arrayInDouble;
        int length;

        if( data.contains(",") ) {
            arrayInString = data.split(",");

            length = arrayInString.length;

            for(int i = 0; i < length; i++) {
                arrayInDouble[i] = Double.parseDouble( arrayInString[i] );
            }
        }

        return arrayInDouble;

    }

I am using Netbeans and the compiler is saying that variable arrayInDouble might not have been initialized. What does that mean?

Justin
  • 24,288
  • 12
  • 92
  • 142
Joshua
  • 305
  • 4
  • 16
  • String[] arrayInString; double[] arrayInDouble; You have not intialized these array that' why you are getting warning that variable may not be initailized. – Rahul Jan 14 '14 at 06:21
  • when I do this 'double[] arrayInDouble = new double[]{};' it fixs the problem, but arrayInString does not complain about it?! – Joshua Jan 14 '14 at 06:22
  • 1
    @Joshua The only time you ever use `arrayInString` is when `data.contains(",")` is true, and in that case, you initialize before using. Thus, the compiler can be sure that you initialize that variable before use. The problem here is that if `data.contains(",")` is false, you're using `arrayInDouble` without initializing (but you wouldn't be using `arrayInString`, hence why it doesn't complain in that way about that variable). – Dennis Meng Jan 14 '14 at 06:25
  • @Dennis Meng Do you mean because I am returning arrayInDouble at the end so I have to assign something to the variable? So the returning behaviour is calling 'using the variable'? or I am wrong? – Joshua Jan 14 '14 at 06:34
  • Pretty much. In the case with the return, what are you returning if the variable isn't initialized? – Dennis Meng Jan 14 '14 at 06:35
  • possible duplicate of ["Variable may not have been initialized"](http://stackoverflow.com/questions/20895175/variable-may-not-have-been-initialized) among numerous others. Also http://stackoverflow.com/questions/19532591/variable-may-not-have-been-intialized covering conditionals – Brian Roach Jan 14 '14 at 06:36
  • Stackoverflow is hard :( even though there is duplicate version doesn't mean I dont need more explaination... – Joshua Jan 14 '14 at 06:40
  • There are so many questions titled `Variable may not have been initialized`, or something similar, that it was difficult to clean up your title. Seriously, before posting a question, go through the other answers (especially the ones that come up when you start typing you question) and **read** them. Stackoverflow is not a random person you ask for an answer. Before asking, see if there is already an answer to your question. If you want more explanation, find some other forum. – Justin Jan 14 '14 at 06:55

4 Answers4

1

In this

String[] arrayInString;
double[] arrayInDouble;

you are just declaring them, before you use them, you also need to initialize:

String[] arrayInString = new String[length1];
double[] arrayInDouble = new double[length2];

where length is the length of the array.

PS: There are other ways to initialize them, for example:

double[] arrayInDouble = {1.1, 2.2}; // The length of the array will be the number of elements in brackets
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • Wired thing: 'String[] arrayInString = new String[3];' it complains 'assigned value never used'... – Joshua Jan 14 '14 at 06:26
  • String is an object, double is primitive, if you're array is of java.lang.Double it will exhibit the same behaviour – hd1 Jan 14 '14 at 06:26
  • @Joshua Is that an error? or a warning? If it's a warning it just means that you are not doing anything with the array `arrayInString`, since this is just for test you can ignore that **warning**. – Christian Tapia Jan 14 '14 at 06:27
  • @Christian Still might want to point out that it isn't ideal to ignore warnings in the long run though. – Dennis Meng Jan 14 '14 at 06:37
1
    String[] arrayInString;

This is not initialized but you provide a value (arrayInString = data.split(",");) before referring to it so there is no complaint.

    double[] arrayInDouble;

This is also uninitialized. If the input does not contain a comma, you skip all the code and then attempt to return the value of arrayInDouble. The warning says that when you execute the return, arrayInDouble may not have a value.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

You need to initialize them (assign some value) before using them.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • when I do this 'double[] arrayInDouble = new double[]{};' it fixs the problem, but arrayInString does not complain about it. Why is that? – Joshua Jan 14 '14 at 06:23
  • @Joshua: Because you are assigning it a value here `arrayInString = data.split(",");` – Ajinkya Jan 14 '14 at 06:31
0

You have not assigned any values to these variables

 String[] arrayInString;
 double[] arrayInDouble;

These variables are local variables so they need to be initialized before using as they will not get any default value for them.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56