Stephanie Loguidice
Stephanie Loguidice

Reputation: 33

Why does netbeans keep telling me that my variables are not being used?

I am a new Java programmer. I was trying to make a program that would add two numbers that the user had to input. For some reason Netbeans keeps telling me that my variables are not being used.

package Calculator;

import java.util.Scanner; 


public class Calculator {

    public static void Main(String[] args) {
        double numOne;
        double numTwo;
        double sum;

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the first number: ");
        numOne = in.nextDouble();
        System.out.println("Please enter the second number: ");
        numTwo = in.nextDouble();
       //code unfinished.

   }

}

Upvotes: 0

Views: 3954

Answers (2)

Kyle Polansky
Kyle Polansky

Reputation: 446

This is happening for different reasons.

For variables numOne and numTwo, you are writing to them in numOne = in.nextDouble(); but you never read that value again. In other words, if your program was finished (I know it isn't), those variables would be pointless as you never perform any calculations on them.

As others have pointed out, the sum variable has not been assigned a value nor used. This variable declaration is unnecessary.

While you are writing your code, you can safely ignore this warnings. However, if you are finished writing your class and you still have these warnings, you should go back and make sure you are actually using your variables. If you never use a variable that you define and you consider your class complete, you should be able to safely delete that variable reference without any adverse effects (although it's always good practice to be careful with those sorts of things).

Upvotes: 1

xrisk
xrisk

Reputation: 3898

Well, the reason NetBeans is telling you so is because you haven't actually used them yet. :) Just reading them in is an indicator to NetBeans that you aren't actually doing anything meaningful with the variables.

Try using them in some calculations or printing them out.

Upvotes: 0

Related Questions