Reputation: 89
Here is my code:
import java.util.Scanner;
public class empMod
{
public static void main(String[] args)
{
int choice;
Scanner input = new Scanner(System.in);
do
{
choice = -1;
System.out.println("Employee Data:");
System.out.println("1. - Employee Name:");
System.out.println("2. - Employee Hire Date:");
System.out.println("3. - Employee Address:");
System.out.println("4. - Employee Number:");
System.out.println("5. - Exit");
choice = input.nextInt();
input.nextLine();
switch (choice)
{
case 1:
String empName = new String ();
System.out.println("Enter the name of the employee:");
String name = input.nextLine();
break;
case 2:
String empDate = new String ();
System.out.println("Enter the hire date of the employee:");
String date = input.nextLine();
break;
case 3:
String empAddress = new String ();
System.out.println("Enter the address of the employee:");
String address = input.nextLine();
break;
case 4:
String empNumb = new String ();
System.out.println("Enter the Employee number:");
int number = input.nextInt();
break;
case 5:
System.out.print("\n");
System.out.println("The name of the employee is: " + empName); // <-- This is the line where the error occurs.
break;
default:
continue;
}
}
while (choice != 6);
}
}
The intent of the program is to have the user input information about the employee, and then at request, have the information displayed. When I go to compile the program, I get the following error:
empMod.java:57: error: variable empName might not have been initialized
System.out.println("The name of the employee is:
" + empName);
^
The string variable is initialized in another case though, so I am not sure of the problem.
Upvotes: 1
Views: 22473
Reputation: 17468
Because int case 5
, you didn't define String empName = new String ();
, you can define empName in main
public static void main(String[] args)
{
int choice;
String empName = "Not a name";
Scanner input = new Scanner(System.in);
...
Upvotes: 0
Reputation: 279910
A switch
block contains a scope. Variables declared in that scope can only be used there. Think of the switch
as multiple if-else
s. If the if
(the case
) that initializes you variable doesn't get executed, then you are left with an uninitialized variable. That's what the compiler is complaining about.
In
case 5:
System.out.print("\n");
System.out.println("The name of the employee is: " + empName); // <-- This is the line where the error occurs.
break;
empName
is only initialized if execution flowed through all the cases (ie. choice
had value 1 and your cases did not have breaks). But the compiler cannot be sure of this.
The way to fix this is to declare your empName
variable outside the switch
block so that its scope is method scope and not limited to inside the switch
. You would need to initialize it to some default value so that the compiler knows that it is initialized.
String empName = "Not a Name";
Upvotes: 0
Reputation: 691655
The empName variable is only initialized in the case 1
section. So what would happen if this block was never executed, and the case 5
section was? What would be printed, since the variable has never been initialized to anything?
Add
String empName = "";
before the loop.
Upvotes: 2
Reputation: 9283
switch (choice)
and case
s later means, that according to value in switch
(choice) one of case
will be choosen. If it will be 5
, your variable will not be initialized.
you need to initialize empName
before switch
, of in every case
in whitch it's used.
And you should not use String empName = new String ();
but String empName = "";
- it will use String Pool.
Upvotes: 1