gpt4 book ai didi

java - Java中Int后无法输入String

转载 作者:行者123 更新时间:2023-12-02 04:20:12 25 4
gpt4 key购买 nike

我在主类中输入 k 和选择(整数)。然后尝试在 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)。

来自Scanner#close() javadoc ,

If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect.

这意味着它将调用输入流的 close() 方法,从而关闭流并断开读取控制台和 java 程序之间的连接。

因此,您不能再在程序中使用 System.in 流(无论您是否使用新的 Scanner() 对象都没有关系)。

关于java - Java中Int后无法输入String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32867976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com