Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文本文件中读取第n个单词 试试看 { BufferedReader br=新的BufferedReader( 新的文件阅读器(selectFile.getSelectedFile()); 字符串str; ArrayList行=新的ArrayList(); 而((str=br.readLine())!=null){ 行。添加(str); } String[]LinesArray=lines.toArray(新字符串[lines.size()]); System.out.println(行);//在屏幕上打印数组列表。 br.close(); } 捕获(IOE异常) { System.out.println(“文件无法读取”); }_Java - Fatal编程技术网

Java 从文本文件中读取第n个单词 试试看 { BufferedReader br=新的BufferedReader( 新的文件阅读器(selectFile.getSelectedFile()); 字符串str; ArrayList行=新的ArrayList(); 而((str=br.readLine())!=null){ 行。添加(str); } String[]LinesArray=lines.toArray(新字符串[lines.size()]); System.out.println(行);//在屏幕上打印数组列表。 br.close(); } 捕获(IOE异常) { System.out.println(“文件无法读取”); }

Java 从文本文件中读取第n个单词 试试看 { BufferedReader br=新的BufferedReader( 新的文件阅读器(selectFile.getSelectedFile()); 字符串str; ArrayList行=新的ArrayList(); 而((str=br.readLine())!=null){ 行。添加(str); } String[]LinesArray=lines.toArray(新字符串[lines.size()]); System.out.println(行);//在屏幕上打印数组列表。 br.close(); } 捕获(IOE异常) { System.out.println(“文件无法读取”); },java,Java,如何打印文件中的第n个单词? 例如,用户运行程序并选择带有段落的文件,我想打印文本文件的第12个和第30个单词 我尝试过使用split,但它对我不起作用。 有什么建议吗?看来你已经把段落排成了一个数组,我的建议是,也许你可以通过数组继续计算空格,当空格计数器达到你想要的数字时(比如第11个),你开始打印单词,同时继续计算空格,在第29个空格后停止打印 Scanner input=新扫描仪(新文件(“someText.txt”); try { BufferedReader br = ne

如何打印文件中的第n个单词? 例如,用户运行程序并选择带有段落的文件,我想打印文本文件的第12个和第30个单词

我尝试过使用split,但它对我不起作用。
有什么建议吗?

看来你已经把段落排成了一个数组,我的建议是,也许你可以通过数组继续计算空格,当空格计数器达到你想要的数字时(比如第11个),你开始打印单词,同时继续计算空格,在第29个空格后停止打印

Scanner input=新扫描仪(新文件(“someText.txt”);
try 
{
    BufferedReader br = new BufferedReader(
            new FileReader(selectFile.getSelectedFile()));
    String str;

    ArrayList <String> lines = new ArrayList<String>();

    while((str = br.readLine()) != null){
        lines.add(str);
    } 
    String[] LinesArray = lines.toArray(new String[lines.size()]);

    System.out.println(lines); //Prints Array list on the screen. 

    br.close();
} 
catch(IOException e) 
{
    System.out.println("The file cannot be read");
}
整数计数=0;
while(input.hasNext()&&count 1.仅粘贴相关代码。2.缩进代码并正确格式化。3.发布一个示例输入文件,输出预期结果,因为文件内容可以转换为stringBuffer。stringBuffer具有检索第n个字符串的良好api。例如,子字符串(x,x+1)@宙斯,这是第n个字符,而不是第n个字符串。@alfasin是的,这是真的,我已经看过了,那么,user3009014,你说它不起作用是什么意思?你可能必须使用带正则表达式的split,使用以下模式。[\s,]+因为单词之间可能有多个空格或多个圆点等,我可能需要更多的解释来正确地帮助你。我如何计算空格?对不起,我一般是编程新手。我同意上面的评论,你需要删掉一些对想帮助你的人没有用的代码。要计算空格,只需遍历数组,使用.equals(“”)将每个元素与空格进行比较。如果.equals语句返回true,则添加1以计数。对于所有的混淆,我感到很抱歉。我认为代码现在更清晰了。谢谢。我将尝试一下,看看它是否有效。
Scanner input = new Scanner(new File("someText.txt"));
int count = 0;

while(input.hasNext() && count <= 30){
    count++;
    String word = input.next();

    if (count == 12){
        System.out.println(word);
    }
    if (count == 30){
        System.out.println(word);
    }
}