0

I have this code here:

    Random rand = new Random();
    int randomNum;
    int i;
    String[] text1 = getResources().getStringArray(R.array.text1);
    String[] text2 = getResources().getStringArray(R.array.text2);



    for(i = 0; i < ((MAX - 1)^(MAX - 1)); i++) {
        randomNum = rand.nextInt(2);
        // True wenn bereits angezeigt worden
        if(shown_table.get(randomNum)) {
            continue;
        }
        // False wenn noch nicht angezeigt worden
        else {
            break;
        }
    }

    // Texte anzeigen und Shown table auf true setzen.
    txt_text1.setText(text1[randomNum]);
    txt_text2.setText(text2[randomNum]);
    //shown_table.put(randomNum, true);

But when I compile it, I get this error:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Error:(118, 33) error: variable randomNum might not have been initialized

when I put the " randomNum = rand.nextInt(2); " before the loop it works... but thats not what I want.

Any ideas why?

ps: I'm new to coding

MrMinemeet
  • 304
  • 6
  • 17
  • 1
    `^` isn't supposed to calculate power set in Java. It's XOR. https://stackoverflow.com/questions/1991380/what-does-the-operator-do-in-java – frogatto Sep 03 '17 at 12:25

2 Answers2

2

XOR has the property that for all x, x^x == 0. So the expression (MAX - 1) ^ (MAX - 1) always evaluates to zero, and your code never enters the loop.

But this is not the problem. The compiler always assumes that the loop may never be executed, and it complains that in this case randomNum will not have been initialized when it is used as an array subscript.

Samuel Peter
  • 4,136
  • 2
  • 34
  • 42
  • Thanks for this information but now I have this problem: `Unable to start activity ComponentInfo{com.example.alex.wouldyoupressthebutton/com.example.alex.wouldyoupressthebutton.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.Hashtable.get(java.lang.Object)' on a null object reference` – MrMinemeet Sep 03 '17 at 12:26
  • @MrMinemeet It looks like `shown_table` is null. – frogatto Sep 03 '17 at 12:29
  • @MrMinemeet Take a look at: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – frogatto Sep 03 '17 at 12:29
  • @HiI'mFrogatto yep.... it's because of the null in the table. when I want to put something into my table it wont work.... I added this in `onCreate`, `shown_table.put(1,true);` and I'm using this to create the hashtable `public Hashtable shown_table;` – MrMinemeet Sep 03 '17 at 12:59
  • @MrMinemeet You are just declaring a variable of type HashTable, but you must actually create an instance. Something like `Map shown_table = new Hashtable<>()` – Samuel Peter Sep 03 '17 at 13:06
  • @SamuelPeter Thanks for this info – MrMinemeet Sep 03 '17 at 13:13
  • "The compiler is smart enough to detect that" no, the compiler is pretty dumb: it simply assumes that the for loop won't be executed, so any value first assigned in the loop is not definitely assigned after. – Andy Turner Sep 03 '17 at 17:15
1

The compiler can't guarantee from the code that 'randomNum' is going to be initialized as the the for loop might be empty. That's why the line txt_text1.setText(text1[randomNum]); should not compile.

If you are sure that the execution will always enter the loop, initialize it with any roughly reasonable value, such as int randomNum=0;

Alexander
  • 2,761
  • 1
  • 28
  • 33