• Register
1 vote
23 views

Problem:

Hello, Kodlogs,

I am a novice in java. I am trying to catch up on things in Java programming language. Thus I write my very first program and try to run it.

But the problem comes when I run my program. It is not giving any output except an error “The value of the local variable is not used”.

So, my question is, where am I doing wrong? How can I fix this error?

public class DataTypes {
    public static void main(String[] args) {
        int x = 12;
        int y = 10;
        double num = 9.20;
        String s = "This is my String";
    }
}

Thanks for your kind help

12 7 7
15,250 points

2 Answers

1 vote

Solution:

Welcome to the colorful programming world.  “The value of the local variable is not used” Is not an error but a warning. It means you declare and initialize your local variables but never used them.

To avoid this just use a print function (at least) thus your IDE can understand you used them.

public class DataTypes {
    public static void main(String[] args) {
        int x = 12;
        int y = 10;
        double num = 9.20;
        double sum =x+y+num;
        String s = "This is my String";
        System.out.println(s + " And my sum is: "+ sum);
    }
}

I hope you understand this now clearly. Thanks.

13 9 6
94,240 points
0 votes

Solution:

Clearly, the error "The value of local variable p is not used.", Is not really an error. It's your IDE (Eclipse), caveat you that you aren't really reading that variable, hence you aren't taking any input from it.

And the other issue with your class is, you don't have a original method. Like this:

public class main {
public static void main(String[] args) {
try {
Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec("calc.exe") ;
} catch(Exception exc){
/*handle exception*/
}
    }
}

And by the way, you must always begin a class name with a captial letter. Hence public class main, must really be public class Main

You obtain that error since you don't have the main method that is used to begin the java program:

public class main {

public static void main(String[] args) {
   try {
       Runtime rt = Runtime.getRuntime() ;
       Process p = rt.exec("calc.exe") ; // here, eclipse is WARINING(so you can ignore it) you that that the variable p is never used(it's just a warning)
   } catch(Exception exc) {
       /*handle exception*/
       // never do this, always put at least a System.out.println("some error here" + e); so you don't ignore a potential exception
   }
}

I conceive what you have is not an error however a caveat; eclipse (and other IDEs/compilers) will inform you that, though you assigned a value to the variable p, you did not exercise it anywhere. It tells you this since this is occasionally an error; in most cases at the time you assign a value to a variable, you later exercise that variable in particular way.

You can extract the error by altering that particular statement to only

rt.exec("calc.exe")

Because you are not necessary to assign a value from the call to exec.

You have identified the variables: xyssnphonenum and twoLine. However, you never exercise the variables anywhere in the code. For example, in case you print the variables out:

System.out.println(x + y + ssn + phone + num + twoLine);

 

10 6 4
31,120 points

Related questions

1 vote
1 answer 20 views
20 views
Problem: Hello experts, I seek your attention. I am a new student in C++ programming. I have installed all the essential software and about the give my first shot using this programming language. Unfortunately, I’ve encountered an error and that says uninitialized local variable ' ... be a noob question. I know that, but I want to know where am I doing the wrong stuff? Thanks for your help.
asked May 19 adamSw 11.3k points
0 votes
2 answers 10 views
1 vote
1 answer 39 views
39 views
Problem: I have a simple python program that supposes to check and compare different numbers. Here is my sample code below myVar1 = 1 myVar2 = 0 def myFunction(): if myVar2 == 0 and myVar1 > 0: print("This is the result of if block") elif myVar1 == ... ‘myVar1’ referenced before assignment. Do you guys have any idea, where I am doing wrong? Please help me to fix the error. Thanks
asked Apr 4 Gavin 15.3k points
0 votes
1 answer 4 views
4 views
Problem: What are local and global objects in C++? What is their difference?
asked Oct 14 Sheeza 2.3k points
0 votes
1 answer 26 views
26 views
Problem: I am naïve to C# programming. I am trying to use the auto-implemented properties. I think the best ever way to fix below error is to declare my own backing variable? Please find below my C# code: public Point Origin { get; set; } ... unchanged. To resolve this error, store the result of the expression in an intermediate value, or use a reference type for the intermediate expression.
asked Jul 23 Raphael Pacheco 4.9k points