Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                                                                                            
                
        
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。
我怎样才能做到这一点?

最佳答案

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

09-26 17:49