【问题标题】:Taking Inputs from BufferReader从 BufferReader 获取输入
【发布时间】:2019-02-19 10:29:28
【问题描述】:

我想从控制台扫描下面的输入,但BufferedReader 在输入 3 行后没有扫描。请帮忙 .... 以下是我的代码

public static void main(String args[]) throws IOException
 {
    root = new trienode();

    System.out.println("enter inputs");
    BufferedReader s = new BufferedReader (new InputStreamReader(System.in));
    String[] s1 = s.readLine().split(" ");
    int  n = Integer.parseInt(s1[0]);
    int  l = Integer.parseInt(s1[1]);

    for(int i=0;i<n;i++)
    {

        String[] s2 = s.readLine().split(" ");
        String key=s2[0];
        int w=Integer.parseInt(s2[1]);
        //Insert(key , w);
    }

    for(int j=0;j<l;j++)
    {
        String s3 = s.readLine();
        if(search(s3)!=-1)
        System.out.println(search(s3));
    }

}

我的输入是

2 1

abc 10

gef 9

ghi

无法扫描ghi...... 请帮忙

【问题讨论】:

  • 函数search有什么作用?需要MCVE
  • 将 for 循环更改为 i&lt;=n,但您仍然会得到 ArrayOutOfBoundException,因为当您阅读 ghi 时,s2[1] 将没有价值
  • 你确定第3行和第4行是用\n字符隔开的
  • 调试,打印 n,l,key,w 并检查它们是否正确
  • 是的,我被骗了,n、l、key、w 的值是正确的......

标签: java input java.util.scanner bufferedreader


【解决方案1】:

在拆分函数中将" " 更改为"\\s+"

public static void main(String args[]) throws IOException {
    root = new trienode();

    System.out.println("enter inputs");
    BufferedReader s = new BufferedReader (new InputStreamReader(System.in));
    String[] s1 = s.readLine().split("\\s+");
    int  n = Integer.parseInt(s1[0]);
    int  l = Integer.parseInt(s1[1]);

    for(int i=0;i<n;i++)
    {

        String[] s2 = s.readLine().split("\\s+");
        String key=s2[0];
        int w=Integer.parseInt(s2[1]);
        //Insert(key , w);
    }

    for(int j=0;j<l;j++)
    {
        String s3 = s.readLine();
        if(search(s3)!=-1)
        System.out.println(search(s3));
    }

}

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 2023-03-30
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多