从第n行开始打印第n个字

时间:2013-11-26 20:18:24

标签: java

int lines = 0;
BufferedReader br = new BufferedReader(new FileReader(selectFile.getSelectedFile()));
Scanner sc = new Scanner(new FileReader(selectFile.getSelectedFile()));
String word = null;

while((word =br.readLine()) != null){
  lines++;
  if(lines == 29){
    System.out.println(word); 
    //Write code to count the word and print the value
    while(sc.hasNext() && count <= 1000){
      count++;
      String value = sc.next();

      if (count == 20){
        System.out.print(value + ",");
      }
    }
  }   
}

我想从第n行开始打印第n个单词。在我的例子中,从第29行开始计算单词并从第29行开始再次打印单词3。 我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

while((word =br.readLine()) != null)
{
    lines++;
    if(lines == n)
    {
        String[] words = lines.split(" ");
        String wordYouSearch = words[n - 1];
        System.out.println(wordYouSearch);
        break;
    }
}