Java 变量未初始化,Void方法无法返回值,无法解析为变量
我试图获取用户输入,以找出用户想要打开的文件的名称,但是在我的setName()方法中,虽然我认为应该是这样,但变量FileName没有被初始化。 getName()方法可能还有另一个问题,但我不确定它是什么。 我能得到一些帮助吗Java 变量未初始化,Void方法无法返回值,无法解析为变量,java,string,variables,input,Java,String,Variables,Input,我试图获取用户输入,以找出用户想要打开的文件的名称,但是在我的setName()方法中,虽然我认为应该是这样,但变量FileName没有被初始化。 getName()方法可能还有另一个问题,但我不确定它是什么。 我能得到一些帮助吗 public void setName(){ Scanner input = new Scanner(System.in); String FileName; boolean done = false; do{#Until the
public void setName(){
Scanner input = new Scanner(System.in);
String FileName;
boolean done = false;
do{#Until the user enters a proper input it should continue to ask for input
System.out.println("Please enter the name of the input file!");
if(input.hasNextLine()){
FileName = input.next();
done = true;
}else{
System.out.println("Please enter a valid name");
}
}while(!done);
return FileName // not being initialized and "Void methods cannot return a value"
}
public String getName(){
return FileName; //The IDE says "FileName cannot be resolved to a variable"
首先,您不应该以大写字母开头变量名(它可以正常工作,但这是bed约定)。其次,您的文件名只存在于setName()方法中,getName()不知道它的存在,它被称为变量范围,您可以在这里了解它。无论如何,你可能想要这样的东西
public class YourClassName {
String fileName;
public String setName(){
Scanner input = new Scanner(System.in);
boolean done = false;
do{
System.out.println("Please enter the name of the input file!");
if(input.hasNextLine()){
fileName = input.next();
done = true;
}else{
System.out.println("Please enter a valid name");
}
}while(!done);
return fileName;
}
public String getName() {
return fileName;
}
}
请注意,文件名是如何在任何方法之外声明的,这种类型的virable称为field。首先,您不应该以大写字母开头variable name(它可以正常工作,但它是bed约定),其次,您的文件名只存在于setName()方法getName()的内部不知道它的存在它被称为变量范围在这里你可以阅读它。无论如何,你可能想要这样的东西
public class YourClassName {
String fileName;
public String setName(){
Scanner input = new Scanner(System.in);
boolean done = false;
do{
System.out.println("Please enter the name of the input file!");
if(input.hasNextLine()){
fileName = input.next();
done = true;
}else{
System.out.println("Please enter a valid name");
}
}while(!done);
return fileName;
}
public String getName() {
return fileName;
}
}
请注意,文件名是如何在任何方法之外声明的,这种类型的virable称为field。您的代码毫无意义。您试图在要设置内容的空白处返回字符串。所以没有理由写
返回文件名无论出于什么原因(因此您有了getName方法)。第二件事,在使用这两种方法之前,需要初始化一个私有字符串文件名。然后在您的方法中,在您说了FileName=input.next()之后,setName()
你需要说this.FileName=FileName
因此您实际上有一个可以使用的变量。以下是代码以及我将如何做到这一点:
private String FileName;
public void setName(){
Scanner input = new Scanner(System.in);
String FileName;
do{#Until the user enters a proper input it should continue to ask for input
System.out.println("Please enter the name of the input file!");
if(input.hasNextLine()){
FileName = input.next();
this.FileName = FileName
}else{
System.out.println("Please enter a valid name");
}
public String getName(){
return FileName;#The IDE says it can't resolve symbol FileName
}
你的代码毫无意义。您试图在要设置内容的空白处返回字符串。所以没有理由写返回文件名无论出于什么原因(因此您有了getName方法)。第二件事,在使用这两种方法之前,需要初始化一个私有字符串文件名。然后在您的方法中,在您说了FileName=input.next()之后,setName()
你需要说this.FileName=FileName
因此您实际上有一个可以使用的变量。以下是代码以及我将如何做到这一点:
private String FileName;
public void setName(){
Scanner input = new Scanner(System.in);
String FileName;
do{#Until the user enters a proper input it should continue to ask for input
System.out.println("Please enter the name of the input file!");
if(input.hasNextLine()){
FileName = input.next();
this.FileName = FileName
}else{
System.out.println("Please enter a valid name");
}
public String getName(){
return FileName;#The IDE says it can't resolve symbol FileName
}
变量FileName
必须以小写字母开头-FileName
。在代码中,此变量是本地变量,但如果要使用getName()
方法,则它必须是字段。您的setName()
方法必须返回字符串。关于这一点:
未初始化文件名
变量FileName
的赋值条件为-if(input.hasNextLine())
。如果条件不满足,则变量未初始化。您需要在声明时或在else
块中初始化变量
所有代码:
public class ClassName {
String fileName;
public String setName() {
Scanner input = new Scanner(System.in);
boolean done = false;
do { // until the user enters a proper input it should continue to ask for input
System.out.println("Please enter the name of the input file!");
if (input.hasNextLine()) {
fileName = input.next();
done = true;
} else {
fileName = "Default";
System.out.println("Please enter a valid name");
}
} while (!done);
return fileName;
}
public String getName() {
return fileName;
}
}
变量FileName
必须以小写字母开头-FileName
。在代码中,此变量是本地变量,但如果要使用getName()
方法,则它必须是字段。您的setName()
方法必须返回字符串。关于这一点:
未初始化文件名
变量FileName
的赋值条件为-if(input.hasNextLine())
。如果条件不满足,则变量未初始化。您需要在声明时或在else
块中初始化变量
所有代码:
public class ClassName {
String fileName;
public String setName() {
Scanner input = new Scanner(System.in);
boolean done = false;
do { // until the user enters a proper input it should continue to ask for input
System.out.println("Please enter the name of the input file!");
if (input.hasNextLine()) {
fileName = input.next();
done = true;
} else {
fileName = "Default";
System.out.println("Please enter a valid name");
}
} while (!done);
return fileName;
}
public String getName() {
return fileName;
}
}
全局声明FileName
。全局声明FileName
。