Java 字符串变量可能尚未初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18550113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String Variable might not have been initialized
提问by Boxasauras
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.
不过,字符串变量在另一种情况下被初始化,所以我不确定这个问题。
回答by dantuch
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.
switch (choice)
和case
s 后面的意思是,根据switch
(选择)中的值,case
将选择其中之一。如果是5
,您的变量将不会被初始化。
you need to initialize empName
before switch
, of in every case
in whitch it's used.
您需要在每次使用empName
之前进行初始化。switch
case
And you should not use String empName = new String ();
but String empName = "";
- it will use String Pool.
你不应该使用String empName = new String ();
但是String empName = "";
- 它会使用字符串池。
回答by JB Nizet
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?
empName 变量仅在case 1
节中初始化。那么如果这个块从来没有被执行过,而这个case 5
部分却被执行了,会发生什么?将打印什么,因为变量从未被初始化为任何东西?
Add
添加
String empName = "";
before the loop.
在循环之前。
回答by Sotirios Delimanolis
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.
一个switch
块包含一个范围。在该范围内声明的变量只能在该范围内使用。将switch
视为多个if-else
s。如果初始化变量的if
(the case
) 没有被执行,那么你就会得到一个未初始化的变量。这就是编译器所抱怨的。
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.
empName
仅在执行流经所有案例时才初始化(即choice
具有值 1 并且您的案例没有中断)。但是编译器不能确定这一点。
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.
解决这个问题的方法是empName
在switch
块外声明你的变量,这样它的范围就是方法范围而不限于switch
. 您需要将其初始化为某个默认值,以便编译器知道它已初始化。
String empName = "Not a Name";
回答by GoingMyWay
Because int case 5
, you didn't define String empName = new String ();
, you can define empName in main
因为 int case 5
,你没有定义String empName = new String ();
,你可以在 main 中定义 empName
public static void main(String[] args)
{
int choice;
String empName = "Not a name";
Scanner input = new Scanner(System.in);
...