0
public class calGrades 
{
    public static void main(String[] args) 
    {
        int choice = 0;
        while(choice != 4)
        {
            int size; 
            Scanner sizeScanner = new Scanner(System.in);

            /* In the grades array, I get an error telling me that the local variable has not 
            yet been initialized. I want to initialize it using the scanner inside the if 
            statement*/ 
            int[] grades = new int[size]; 

            System.out.println("-----MENU-----");
            System.out.println("1) Enter size");
            System.out.println("2) Enter grades");
            System.out.println("3) Calculate average");
            System.out.println("4) Exit program.");

            Scanner choiceScanner = new Scanner(System.in);
            choice = choiceScanner.nextInt();

            if(choice == 1)
            {
                System.out.println("Enter size");
                sizeScanner = new Scanner(System.in);
                size = sizeScanner.nextInt();
            }
            if(choice == 2)
            {
                /*If i initialize size = 0, and then try the print statement 
                below, it will print out a 0 instead of whatever is read 
                into it via the scanner. */
                System.out.println(size)
                System.out.println("Enter grades: "); 
                Scanner gradeScanner = new Scanner(System.in);
                for(int i = 0; i < size; i++) //I get the same error here in my for loop.
                {
                    grades[i] = gradeScanner.nextInt();
                }
            }
        }
    }
}

I am trying to initialize variable 'size' inside of the first 'if' statement. If i initialize the variable in there, it does not change the actual value of size. I undertand that it is outside of the scope, but I do not know how to get it to store in there. This also creates problems with my grades array since I need to know the size of 'size' to set the size of the array. The error I get is: The local variable size may not have been initialized Java(536870963)

  • 1
    Most people will use `int size = -1;` using what programmers refer to as a [magic number](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad). Then later, if you need, you can check (guard) for the `-1` value in code to ensure your program works. – tresf Sep 29 '22 at 04:20
  • You might also want to declare `size` outside the loop – Thum Choon Tat Sep 29 '22 at 04:25
  • 1
    Please don't make multiple scanners for `System.in`. If you want to read from `System.in`, then create your `Scanner` right at the beginning of your program, and just use the same `Scanner` over and over. – Dawood ibn Kareem Sep 29 '22 at 04:25
  • @tresf the issue with that is that I need size to be a positive integer, since it dicatates the size of the array used. Size has to be greater than or equal to 1, but if I initiailize it in the beginning, then it wont change after a value is inputed via the scanner. – TheTargetAudience Sep 29 '22 at 04:43
  • @ThumChoonTat I have tried that and I still run into the same error. The issue is with trying to initialize it in the first if-statement. I need the size to be set as the input being taken in. – TheTargetAudience Sep 29 '22 at 04:44
  • You declared `size` inside your `while` loop. Next time through the loop you get a new `size` variable, and the old value is lost. Declare `size` *before* the `while` loop instead. – Ole V.V. Sep 29 '22 at 04:52
  • @OleV.V. Awesome. That worked! Thank you so much! I have another question. Would my grades[] array also go on the outside? I've tried putting it inside and outside the while loop but I can't get it to properly store data. If I have it outside the while loop,I get an out of bounds error after the first entry. If i keep it inside the while loop, It takes in all the data for the size of the arrat but sets it to 0. Think you could help me on that? – TheTargetAudience Sep 29 '22 at 05:25
  • It needs to go outside so you can calculate the average after the user has previously entered the grades. However you can instantiate it (`grades = new int[size]`) only after the user has entered a size, so somewhere inside the loop. – Ole V.V. Sep 29 '22 at 05:31
  • @OleV.V. I was able to get it fixed. You were a big help on helping me understand this a bit more. Thanks so much! – TheTargetAudience Sep 29 '22 at 05:48

0 Answers0