Вот мой код:Строка переменной не может быть инициализирован
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);
}
}
Целью программы является наличие пользовательского ввода информации о работнику, а затем по желанию, есть информация, отображаемая. Когда я скомпилировать программу, я получаю следующее сообщение об ошибке:
empMod.java:57: error: variable empName might not have been initialized
System.out.println("The name of the employee is:
" + empName);
^
переменная строка инициализируется в другом случае, хотя, так что я не уверен, что проблемы.
@SotiriosDelimanolis Так что, если у меня есть код распечатать информацию за пределами заявления выключателя, будет та работа? – Boxasauras