【发布时间】:2013-12-02 05:56:56
【问题描述】:
我无法确定在使用 try 和 catch 处理时应该将什么放置在什么范围内,我的部分代码要求用户输入他们想要扫描的文件名,这是 try 和捕获语句:
try{
System.out.println("Enter the name of the file you wouldlike to scan: ");
String fileName = scan.nextLine();
File file = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(fileName) }
catch( IOException ioe){
System.out.println(ioe);
System.exit(-1);
}
当我编译它在“line = br.readLine();”中找不到符号“br” //扫描文件的部分代码,不知道在try语句的哪个作用域里放什么
这是代码的另一个(不是整个程序)部分,我用 system.in 测试了这部分并且工作正常,但不能与 filereader 一起工作
String line;
int lineCount = 0, wordCount = 0, charCount = 0, palCount = 0;
int thisLineWords = 0, thisLineChars = 0;
boolean isPal = true;
do {
try {
line = br.readLine();
}
catch( Exception e ) {
break;
}
if( line == null ) break;
if( line.compareTo(".") == 0 ) break;
thisLineWords = 0;
thisLineChars = line.length() + 1; // count chars
isPal = true;
//count words
boolean inWord = false;
for( int i = 0; i < line.length(); i++ ) {
char ch = line.charAt(i);
if( Character.isWhitespace(ch) ) {
if( inWord ) inWord = false;
}
else {
if( !inWord ) {
inWord = true;
thisLineWords++;
}
}
}
【问题讨论】:
-
缺少一个 );在新的 BufferedReader 行的末尾
-
您可以将try语句中的代码发布到
line = br.readLine();吗?