• Register
1 vote
805 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

Please log in or register to answer this question.

2 Answers

2 votes

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,260 points
Great answer
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

0 votes
1 answer 46 views
46 views
Problem: Hi! I am a novice and trying to catch up the concepts in Java. I tried to access a local variable without assigning it a value in main() method and got following error: error: variable might not have been initialized Meanwhile, if I declare the ... variable or the same value is assigned to all variables regardless of datatype? If any of you could explain this, I would be thankful.
asked Nov 29, 2020 Code Learner 9.8k points
1 vote
1 answer 79 views
79 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, 2020 adamSw 11.3k points
0 votes
1 answer 37 views
37 views
Problem: Hi there! I am trying to run the following program which is giving the above-mentioned warning: #include<iostream> #include<conio.h> using namespace std; int add(int number1, int number2) { int result = number1 + number2; return &result; } int subtract (int number1, ... When I run this program, it gives the same warning on different lines. I do not know how to solve this issue. Any help?
asked Feb 24 Code Learner 9.8k points
0 votes
1 answer 7 views
7 views
Problem: How I can fix this issue: What are local and global variable in python?
asked Mar 17 Wafa Abu Yousef 6.1k points
0 votes
1 answer 40 views
40 views
Problem: The following code gives the error UnboundLocalError: local variable 'Var1' referenced before assignment: Var1 = 1 Var2 = 0 def function(): if Var2 == 0 and Var1 > 0: print("Result One") elif Var2 == 1 and Var1 > 0: print("Result Two") elif Var1 < 1: print("Result Three") Var1 =- 1 function() How can I fix this? Thanks for any help!
asked Jan 8 Mashhoodch 13k points
0 votes
2 answers 28 views
1 vote
1 answer 92 views
92 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 == ... &lsquo;myVar1&rsquo; 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, 2020 Gavin 15.3k points
0 votes
1 answer 12 views
12 views
Problem: If I have a class OnlineDataRetrieval which I need to use time to time in my Activity, is it wise to open a global object for that class and reuse that object throughout the lifespan of the activity, Or I should just create a local Object every time I need to use that class? So basically I want to know which of the options below is preferable from performance point of view?
asked Dec 5, 2020 Mashhoodch 13k points
0 votes
1 answer 44 views
44 views
Problem: What are local and global objects in C++? What is their difference?
asked Oct 14, 2020 Code Learner 9.8k points
0 votes
1 answer 4 views
4 views
Problem: Hi there! I recently started learning the programming and I was attempting an onliner quiz in which I came across following multiple choice question: A locally declared variable always ____ another variable with the same name elsewhere in the class. I do not remember ... wrong option for this one. I want to know the correct answer and the meaning of this question with examples. Thank you!
asked Apr 21 Code Learner 9.8k points