Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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中使用String.split方法并将令牌放入数组中_Java_String_Split - Fatal编程技术网

在Java中使用String.split方法并将令牌放入数组中

在Java中使用String.split方法并将令牌放入数组中,java,string,split,Java,String,Split,我不明白为什么我下面的代码不起作用。我尝试使用缓冲读取器读取文本文件,并拆分第一行,然后将每个单词放入数组中 System.out.println打印出输入文本中的所有数组项,而不是第一个单词。我不明白为什么。非常感谢您的帮助 String [] words = line.split(" "); System.out.println(words[0]); 您的代码中似乎有一些问题,在中有一个方便的方法,然后类似这样的东西 BufferedReader input = new Buffere

我不明白为什么我下面的代码不起作用。我尝试使用缓冲读取器读取文本文件,并拆分第一行,然后将每个单词放入数组中

System.out.println打印出输入文本中的所有数组项,而不是第一个单词。我不明白为什么。非常感谢您的帮助

String [] words = line.split(" "); 
System.out.println(words[0]); 

您的代码中似乎有一些问题,在中有一个方便的方法,然后类似这样的东西

BufferedReader input = new BufferedReader(new FileReader(inputFile));
String line = input.readLine(); // Java variable names should be lower case.
                                // Constants are ALL CAPS. Please follow conventions.  
String [] words = line.split(" "); // words, not Words - and line (not firstLine). 
System.out.println("Line : " + Arrays.toString(words)); //  Arrays.toString(words)
                      // not words[100]... which would be the 101st word (which
                      // is a very long line).

你能更具体地说明什么不起作用吗?我的输入文件只包含3个单词,但当我试图打印出单词[0]、[1]和[100]时,它们都给出了相同的结果,即输入文件中的所有三个单词。看来文字还没放进阵中,多谢你的帮助!我已经更改了变量的名称。我之所以输入单词[100],是因为我的输入文件只有三个单词(即,一个文本文件中的单词:蓝-红-黄)。但它还是把这三个都打印出来了。我尝试了单词[0]和[1],它给出了相同的结果。看来这些词还没有放到数组中?再次感谢。你确定每个单词之间都有空格,而不是其他字符(如制表符)吗?