Reputation: 1
getting error that variable not found. not able to figure out exact problem. tried resetting preferences also in bluej.
import java.util.*;
class Electricity
{
public void Initialization()
{
int omr = 0;
int nmr = 0;
int cr = 0;
int rent = 0;
double cost = 0.0;
double sc = 0.0;
}
public void input()
{
System.out.println("Enter old meter reading");
omr = sc.nextInt();
System.out.println("Enter new meter reading");
nmr = sc.nextInt();
}
public void calculate()
{
cr = nmr - omr;
}
public static void main()
{
Scanner sc = new Scanner(System.in);
}
}
Upvotes: 0
Views: 589
Reputation: 76
Somthing like that.
import java.util.Scanner; // Import the Scanner class
public class Main
{
static int calculate(int nmr, int omr)
{
int cr = nmr - omr;
return cr;
}
public static void main(String[] args)
{
int omr = 0;
int nmr = 0;
// int cr = 0;
// int rent = 0;
// double cost = 0.0;
// double sc = 0.0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter old meter reading");
omr = sc.nextInt();
System.out.println("Enter new meter reading");
nmr = sc.nextInt();
System.out.println("The resaults: " + calculate(nmr, omr));
}
}
Upvotes: 1
Reputation: 1678
This is not error in bluej it's problem with the code
I'm guessing you are a student
The code is not working since variable only exist inside the method it was created in
You are trying to access variable defined in different methods.
Inside the input()
method you trying to access variable that you defined in Initialization()
method
But it does not exist in input()
Jest to make this code work put all your code inside the main()
method .
Or pass the method the variables they using from another method and of course you need to call those methods from main method but seems like you have some more learning to do to make the second option to work.
Good luck.
Upvotes: 2