-2

I've been trying the following code to get a char with Scanner:

    import java.util.Scanner;

    public class PhoneBill
    {
        public static void main(String[] args)
        {

           // Create Scanner
           Scanner input = new Scanner (System.in);

           //Declare variables
           char r, p;
           double minutes, nightm, daym, totalcost, account;

           //Obtain account number
           account = input.nextDouble(); 

           //Determine service
           System.out.println("Enter r for regular service or p for premium service: ");
           char service = input.next(".").charAt(0);

           //Logic for regular service
           if (service == r)
           {
               System.out.println("Enter number of minutes talked: ");
               minutes = input.nextDouble();

               if (minutes >= 50)
                   totalcost = (10.00 + (minutes * 0.20));
           }  

           //Display account
           System.out.println ("The account number is: " + account);

           //Display service type


          }
    }

but I received this error:

PhoneBill.java:29: error: variable r might not have been initialized         
if (service == r)    
               ^
1 error

Where is the issue and how to do it in a proper way?

m.antkowicz
  • 13,268
  • 18
  • 37
  • 4
    Have you tried assigning a value to `r`? e.g. `r = 'r'`. Like the error message says, you haven't done so yet, and you will need to before you can reference it. Or even, you could simply use `if (service == 'r')`. – Andy Turner Sep 17 '15 at 20:49

4 Answers4

0

You have to initialize your variables (both objects and primitive types) before using them in an operation. Define r and p as

char r = 'r', p = 'p'; // 
Sedat Polat
  • 1,631
  • 2
  • 18
  • 28
0

You declared a variable r and you didn't give it any value. Then you trying to compare service against an uninitialized variable. So what you need is to give values to your r and p variables before using them.

Amr
  • 792
  • 3
  • 15
0

You have declared a variable r, and will need to assign a value to it before you can use it:

char r = 'r';

In this case, where you always will enter r for regular service etc (at least until you change the instruction message), you could simply use the character literal r instead of having a variable:

if (service == 'r') {
  // Do something.
}

However, to make your code easier to change, you might still want to use a variable, in order that you can update it in one place and have it automatically update all of the relevant locations.

In this case, you can declare it as:

final char r = 'r';

The final is optional, but prevents you from accidentally changing it. Using a longer variable name to describe what it is for is a good idea too, e.g. regular instead of r.

Then you can write:

System.out.println("Enter " + regular + " for regular service...");
char service = /* ... Whatever ... */ ;
if (service == regular) {
  // ... Whatever.
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

Local variables must be initialized before they are used (mandated by the Java language specification), meaning that there must not exist any execution path which will lead to using an uninitialized variable.

To get rid of the compilation error, you could do:

char r = 0, p = 0;

Although we can argue whether this should be handled better in the Java language in general, the compiler did you a favour here: You probably have a bug in your logic because you are using a variable which is not initialized anywhere.

Community
  • 1
  • 1
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110