代码之家  ›  专栏  ›  技术社区  ›  user225269

如何在Java文本文件中搜索某个单词

  •  0
  • user225269  · 技术社区  · 15 年前

    如何在Java文本文件中搜索某个单词? 使用缓冲读卡器,我有这个代码,但我得到一个

    java.lang.ArrayIndexOutOfBoundsException
    

    请帮助我确定我的程序出了什么问题。

    System.out.println("Input name: ");
          String tmp_name=input.nextLine();
    
    
            try{
    
                 FileReader fr;
          fr = new FileReader (new File("F:\\names.txt"));
          BufferedReader br = new BufferedReader (fr);
    String s;
    while ((s = br.readLine()) != null) {
    
    String[] st = s.split(" ");
    String idfromtextfile=st[0];
    String nemfromtextfile = st[1];
    String scorefromtextfile=st[2];
    
    
    if(nemfromtextfile.equals(tmp_name)){
        System.out.println("found");      
    }else{
        System.out.println("not found");
    }
    
    
    
          }
    
      }catch(Exception e){ System.out.println(e);}
    

    name.txt如下所示:

    1
    a
    0
    
    2
    b
    0
    
    4 回复  |  直到 15 年前
        1
  •  2
  •   fstab    15 年前

    1 a 0
    2 b 0
    

    ArrayIndexOutOfBoundsException

    if ( st.length != 3) {
        System.err.println("The line \"" + s + "\" does not have three space-separated words.");
    }
    
        2
  •  0
  •   Sean Patrick Floyd    15 年前

    here Scanner

    BufferedReader in
       = new BufferedReader(new FileReader("foo.in"));
    

    in.toString()

        3
  •  0
  •   Gadolin    15 年前

        4
  •  0
  •   Suresh Kumar    15 年前