[英]Cannot able to input String after Int in Java
我在Main类中输入k和choice(整数)。 然后尝试在count类中输入title_name。 但是,我无法输入title_name。
//主类
package com.iiitd.ap.lab6;
import java.io.IOException;
import java.util.Scanner;
public class Main {
static int k;
static int option;
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
k=in.nextInt();
option=in.nextInt();
in.close();
System.out.println(k+" "+option);
count t=new count(k,option);
t.count_print();
}
}
//计数类
package com.iiitd.ap.lab6;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class count {
static int k;
static int option;
count(int k, int option)
{
count.k= k;
count.option= option;
}
int decide_file_1() throws IOException
{
String title_name="";
Scanner in=new Scanner(System.in);
title_name=in.next();
System.out.println("ttt");
in.close();
for(int i=1;i<=20;++i){
FileInputStream fs= new FileInputStream("/home/tarun/Downloads/Lab6/Papers/paper"+i+".txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
System.out.println(title_name+" "+br.readLine());
if(title_name.equals(br.readLine()))
return i;
}
return 0;
}
int count_print() throws IOException
{
if(option==1)
{
System.out.printf("decide_file=%d",decide_file_1());
}
return 0;
}
}
任何帮助将不胜感激。
您正在通过调用in.close()
关闭标准输入流( System.in
in.close()
。
如果尚未关闭此扫描器,则如果其基础可读文件也实现了Closeable接口,则将调用该可读文件的close方法。 如果此扫描仪已经关闭,则调用此方法将无效。
这意味着它将通过关闭输入流并断开读取控制台与Java程序之间的连接来调用输入流的 close()方法。
因此,您不能再在程序中使用System.in
流(无论您是否使用新的Scanner()对象都没有关系)。
您正在两次阅读该行:
System.out.println(title_name+" "+br.readLine());
if(title_name.equals(br.readLine()))
return i;
在打印和比较之前存储值。 阅读该行后,读者便会前进。 因此,第二个readLine()调用正在获取流中的下一行。
String title = br.readLine();
System.out.println(title_name+" "+title);
if(title_name.equals(title))
return i;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.