gpt4 book ai didi

java - 打印从第 n 行开始的第 n 个单词

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

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;
}
}

关于java - 打印从第 n 行开始的第 n 个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20227123/

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