-3

Ok, so I searched for a similar thread like this one but couldn't find the answer I'm looking for.

I'm trying to program something that generates random numbers and turns them into a question. I'm not even sure it's even properly written but I'm having a compilation error.

"Variable answer1 might not have been initialized"

Here's the code :

import java.util.Scanner;
import java.util.Random;

public class random{
    public static void main (String [] args){

        System.out.println("Random number generated");

        Random obj= new Random();
        Scanner scan = new Scanner(System.in);
        int answer1;
        int rgen= obj.nextInt(100);
        int rgen1= obj.nextInt(1000);


        System.out.println(rgen + " + " + rgen1 + " = ? ");
        scan.nextInt();
        if (answer1 == rgen + rgen1)
        System.out.println("Correct");
        else
        System.out.println("Wrong");


    }
}

3 Answers3

2

Define answer1 to be some initial value.

int answer1 = 0;

You can't use a variable that you haven't initialized, which is what you attempt to do with if (answer1 == rgen + rgen1).

It'd likely be that you want to read in the next integer, so you could also do this:

int answer1 = scan.nextInt();

Or, before you hit your if block, you can change the statement of scan to put the value into the variable instead:

answer1 = scan.nextInt();
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • How do I do that? And why do people vote down my thread? :/ --EDIT -- oh thanks! – Edward Hiroko May 15 '13 at 15:28
  • @EdwardHiroko "int answer1 = 0;" – Kevin Nelson May 15 '13 at 15:29
  • Any idea how to make my random generator last forever? if the user inserts the wrong answer it will print the same question and if the user has inserted the correct one, then the system will generate another random math question... ? possible? – Edward Hiroko May 15 '13 at 15:48
2

You probably want this:

answer1 = scan.nextInt();

Otherwise, the call of scan.nextInt() reads and discards the value, while answer1 remains uninitialized. You should probably combine initialization with declaration, too: remove

int answer1;

and replace it with

int answer1 = scan.nextInt();

on the line where you read the int from the Scanner.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The code

int answer1;

reserves space on the stack for that variable. It doesn't put anything in there, so whatever is in that variable is whatever was in memory at that location at the time.

It hasn't been initialized because you haven't put anything in there yet. The warning will go away if you change it to

int answer1 = 0;

But honestly I'm not sure what your code is trying to do.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
Mike B
  • 478
  • 2
  • 11
  • Thanks! it works, and my code is trying to generate random numbers and then prints them in a math question like "50 + 400", now if the answer is 450 then it'll print "correct", if it's not, it'd print "wrong". – Edward Hiroko May 15 '13 at 15:35