Java 为什么此功能不能在;如果;陈述
我想原因可能是我没有关闭“tf4”的扫描仪。如果我关闭它,然后编辑它,一切正常,但当我打开它时,系统会发送“错误”:系统会说:异常 线程“main”中的java.lang.illegalstateexception扫描程序已关闭 用于生成tf4的代码:Java 为什么此功能不能在;如果;陈述,java,if-statement,Java,If Statement,我想原因可能是我没有关闭“tf4”的扫描仪。如果我关闭它,然后编辑它,一切正常,但当我打开它时,系统会发送“错误”:系统会说:异常 线程“main”中的java.lang.illegalstateexception扫描程序已关闭 用于生成tf4的代码: public static String[] txtDocument(String args1) throws Exception{ Scanner scannerTXT = new Scanner(new File(file1));
public static String[] txtDocument(String args1) throws Exception{
Scanner scannerTXT = new Scanner(new File(file1));
String[] list4 = new String[2];
int count3=0;
while (scannerTXT.hasNextLine()) {
String data = scannerTXT.nextLine();
if(data.contains(args1)){
count3++;
String list1 = data;
for(int i_t1=0;i_t1<2;i_t1++){
list4 = list1.split(",");
break;
}
}
if(count3==0){
list4[0]="";
list4[1]="";
}
scannerTXT.close();
}
return list4;
}
2) 但如果我将其置于if-else条件下,它总是返回:“无法删除文件”
谁能告诉我为什么?这太奇怪了 尝试此更改:
public static String[] txtDocument(String args1) throws Exception{
Scanner scannerTXT = new Scanner(new File(file1));
String[] list4 = new String[2];
int count3=0;
while (scannerTXT.hasNextLine()) {
String data = scannerTXT.nextLine();
if(data.contains(args1)){
count3++;
String list1 = data;
for(int i_t1=0;i_t1<2;i_t1++){
list4 = list1.split(",");
break;
}
}
if(count3==0){
list4[0]="";
list4[1]="";
}
// Not here... scannerTXT.close();
}
scannerTXT.close(); // But here
return list4;
}
public静态字符串[]txtDocument(字符串args1)引发异常{
scannerScannerTXT=新扫描仪(新文件(file1));
字符串[]列表4=新字符串[2];
int count3=0;
while(scannerTXT.hasNextLine()){
字符串数据=scannerTXT.nextLine();
if(data.contains(args1)){
count3++;
字符串list1=数据;
对于(int i_t1=0;i_t1)在发布这段代码之前,您是否调试过这段代码以获取答案?我们没有办法提供帮助。对于初学者,我们现在对tf4一无所知,因此甚至不能冒险猜测您的问题可能是TestMain在同一个文件上运行,因此文件被new TestMain
锁定了吗?您是否在new TestMain()
之后尝试调用k.removeLineFromFile()
?在这种情况下是否有效?@nielsen非常感谢您的回复。我试图关闭TestMain文件,但没有更好的结果“Testmain tf3=new Testmain();”是一个错误。很抱歉,我已将其删除。看起来我无法直接调用“k.removeLineFromFile()"…非常感谢您的帮助和耐心。我非常感谢。我创建这个类是为了测试,我只是从其他类文件复制了所需的代码。我刚刚重命名了另一个文件,最后……正如您所说,我应该关闭该文件。@Delibz-如果这是解决您问题的答案,您应该将其标记为已接受,以便其他人接受我可以看出这是有帮助的。
public class cs1{
public static void main (String[] args) throws Exception{
cs1 k = new cs1();
if(tf4[1].length()!=0){
if(!tf4[1].trim().equals(tf5[3].trim())){
***k.removeLineFromFile(file1,args[0]);***
...............
public static String[] txtDocument(String args1) throws Exception{
Scanner scannerTXT = new Scanner(new File(file1));
String[] list4 = new String[2];
int count3=0;
while (scannerTXT.hasNextLine()) {
String data = scannerTXT.nextLine();
if(data.contains(args1)){
count3++;
String list1 = data;
for(int i_t1=0;i_t1<2;i_t1++){
list4 = list1.split(",");
break;
}
}
if(count3==0){
list4[0]="";
list4[1]="";
}
// Not here... scannerTXT.close();
}
scannerTXT.close(); // But here
return list4;
}