java.util.Scanner 此处不允许对象声明
java.util.Scanner Object Declaration not Allowed Here
我必须在不同的范围内使用 Scanner 对象,如下所示:
public String fidentifier (String u)
{
try {
Scanner t = new Scanner( new File( "ubasic.dat") );
//Some Statements
} catch( FileNotFoundException e ){
System.out.println( "Exception : " + e );
}
}
public String didentifier(String cat)
{
try {
if( cat.equals("Government") )
Scanner s = new Scanner( new File("ugov.dat") );
else
Scanner s = new Scanner( new File("uhc.dat") );
//Some Statements
} catch( FileNotFoundException e ) {
System.out.println( "Exception : " + e );
}
}
由于我在两种不同的方法中明确声明了 Scanner 对象,我仍然收到错误,指出方法 didentifier()
中不允许声明 Scanner 对象。
指出我错的地方。
您可以根据需要使用和声明任意数量的扫描仪...(这是一种不好的做法,但对此没有技术限制...)
...Point me out where I'm wrong....
错误是:扫描仪无法解析为变量
错误的原因是您试图在 if else 范围内声明一个对象,但没有使用花括号 { }
为此替换方法中的代码:
if (cat.equals("Government")) {
Scanner s = new Scanner(new File("ugov.dat"));
} else {
Scanner s = new Scanner(new File("uhc.dat"));
// Some Statements
}
一切都会好起来的...
最后你可以拥有一个全局 Scanner 对象,你只需要更改对象的引用即可。
public String didentifier(String cat)
{
try
{
if( cat.equals("Government") )
s = new Scanner( new File("ugov.dat") );
else
s = new Scanner( new File("uhc.dat") );
//Some Statements
}catch( FileNotFoundException e ) {
System.out.println( "Exception : " + e );
}
//your Return here...
}
if
之后的语句有它自己的作用域。在下一行声明一个不存在的变量是没有意义的,因为它超出了范围。最简单的做法是使用变量或 ?:
String file;
if( cat.equals("Government") )
file = "ugov.dat";
else
file = "uhc.dat";
Scanner s = new Scanner( new File(file) );
或
Scanner s = new Scanner( new File(cat.equals("Government") ? "ugov.dat": "uhc.dat" ) );
我必须在不同的范围内使用 Scanner 对象,如下所示:
public String fidentifier (String u)
{
try {
Scanner t = new Scanner( new File( "ubasic.dat") );
//Some Statements
} catch( FileNotFoundException e ){
System.out.println( "Exception : " + e );
}
}
public String didentifier(String cat)
{
try {
if( cat.equals("Government") )
Scanner s = new Scanner( new File("ugov.dat") );
else
Scanner s = new Scanner( new File("uhc.dat") );
//Some Statements
} catch( FileNotFoundException e ) {
System.out.println( "Exception : " + e );
}
}
由于我在两种不同的方法中明确声明了 Scanner 对象,我仍然收到错误,指出方法 didentifier()
中不允许声明 Scanner 对象。
指出我错的地方。
您可以根据需要使用和声明任意数量的扫描仪...(这是一种不好的做法,但对此没有技术限制...)
...Point me out where I'm wrong....
错误是:扫描仪无法解析为变量 错误的原因是您试图在 if else 范围内声明一个对象,但没有使用花括号 { }
为此替换方法中的代码:
if (cat.equals("Government")) {
Scanner s = new Scanner(new File("ugov.dat"));
} else {
Scanner s = new Scanner(new File("uhc.dat"));
// Some Statements
}
一切都会好起来的...
最后你可以拥有一个全局 Scanner 对象,你只需要更改对象的引用即可。
public String didentifier(String cat)
{
try
{
if( cat.equals("Government") )
s = new Scanner( new File("ugov.dat") );
else
s = new Scanner( new File("uhc.dat") );
//Some Statements
}catch( FileNotFoundException e ) {
System.out.println( "Exception : " + e );
}
//your Return here...
}
if
之后的语句有它自己的作用域。在下一行声明一个不存在的变量是没有意义的,因为它超出了范围。最简单的做法是使用变量或 ?:
String file;
if( cat.equals("Government") )
file = "ugov.dat";
else
file = "uhc.dat";
Scanner s = new Scanner( new File(file) );
或
Scanner s = new Scanner( new File(cat.equals("Government") ? "ugov.dat": "uhc.dat" ) );