Java 当某些单词位于同一行时,计算ArrayList中的单词数
我试图计算一个ArrayList包含多少单词。如果每个单词都在单独的一行上,我知道怎么做,但有些单词在同一行上,比如:Java 当某些单词位于同一行时,计算ArrayList中的单词数,java,Java,我试图计算一个ArrayList包含多少单词。如果每个单词都在单独的一行上,我知道怎么做,但有些单词在同一行上,比如: hello there blah cats dogs 所以我想我应该检查每个条目,找出当前条目包含多少单词,比如: public int numberOfWords(){ for(int i = 0; i < arraylist.size(); i++) { int words = 0; words = words + (
hello there
blah
cats dogs
所以我想我应该检查每个条目,找出当前条目包含多少单词,比如:
public int numberOfWords(){
for(int i = 0; i < arraylist.size(); i++) {
int words = 0;
words = words + (number of words on current line);
//words should eventually equal to 5
}
return words;
}
public int numberOfWords(){
对于(int i=0;i
我想得对吗 您应该在循环外部声明并实例化
int words
,int
在循环的每次迭代中都不会重新分配。您可以使用for..each
语法在列表中循环,这将消除从列表中获取()项的需要。要处理一行中的多个单词,请将字符串拆分为数组,并对数组中的项目进行计数
public int numberOfWords(){
int words = 0;
for(String s:arraylist) {
words += s.split(" ").length;
}
return words;
}
全面测试
public class StackTest {
public static void main(String[] args) {
List<String> arraylist = new ArrayList<String>();
arraylist.add("hello there");
arraylist.add("blah");
arraylist.add(" cats dogs");
arraylist.add(" ");
arraylist.add(" ");
arraylist.add(" ");
int words = 0;
for(String s:arraylist) {
s = s.trim().replaceAll(" +", " "); //clean up the String
if(!s.isEmpty()){ //do not count empty strings
words += s.split(" ").length;
}
}
System.out.println(words);
}
}
公共类堆栈测试{
公共静态void main(字符串[]args){
List arraylist=新建arraylist();
arraylist.add(“你好”);
arraylist.添加(“废话”);
arraylist.add(“猫狗”);
arraylist.add(“”);
arraylist.add(“”);
arraylist.add(“”);
int字=0;
for(字符串s:arraylist){
s=s.trim().replaceAll(“+”,“”);//清理字符串
如果(!s.isEmpty()){//请不要计算空字符串
文字+=s.split(“”)长度;
}
}
System.out.println(字);
}
}
应如下所示:
public int numberOfWords(){
int words = 0;
for(int i = 0; i < arraylist.size(); i++) {
words = words + (number of words on current line);
//words should eventually equal to 5
}
return words;
}
public int numberOfWords(){
int字=0;
对于(int i=0;i
我想这会对你有所帮助
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class LineWord {
public static void main(String args[]) {
try {
File f = new File("C:\\Users\\MissingNumber\\Documents\\NetBeansProjects\\Puzzlecode\\src\\com\\test\\test.txt"); // Creating the File passing path to the constructor..!!
BufferedReader br = new BufferedReader(new FileReader(f)); //
String strLine = " ";
String filedata = "";
while ((strLine = br.readLine()) != null) {
filedata += strLine + " ";
}
StringTokenizer stk = new StringTokenizer(filedata);
List <String> token = new ArrayList <String>();
while (stk.hasMoreTokens()) {
token.add(stk.nextToken());
}
//Collections.sort(token);
System.out.println(token.size());
br.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.util.StringTokenizer;
公共类行字{
公共静态void main(字符串参数[]){
试一试{
文件f=新文件(“C:\\Users\\MissingNumber\\Documents\\NetBeansProjects\\Puzzlecode\\src\\com\\test\\test.txt”);//创建指向构造函数的文件传递路径。。!!
BufferedReader br=新的BufferedReader(新文件读取器(f))//
字符串strLine=“”;
字符串filedata=“”;
而((strLine=br.readLine())!=null){
filedata+=strLine+“”;
}
StringTokenizer stk=新的StringTokenizer(文件数据);
列表标记=新的ArrayList();
while(stk.hasMoreTokens()){
token.add(stk.nextToken());
}
//集合。排序(令牌);
System.out.println(token.size());
br.close();
}捕获(例外e){
System.err.println(“错误:+e.getMessage());
}
}
}
因此,在本例中,您将从文件中提取数据,并在标记化后将其存储在列表中,只需对其进行计数,如果您只想从控制台获取输入,请使用Bufferedreader,标记化它们,用空格分隔,放入列表,简单获取大小
希望你得到了你想要的。先用行分割,然后每行用空格。单词如何以空格开头?您可以使用String.split()
,但可能有更好的方法。注意带空格的行!这将为您提供“1”:System.out.println(“.split(“.length”)代码>(拆分正则表达式中应该有多个空格,但我无法说服SO中的autoformat显示it@Miquel我没有想到,新的更新使用了trim()
,谢谢!实际上我现在正在研究一些东西。结果是System.out.println(“.trim().split(”).length)
给你1,而这个System.out.println(“.split(“.length”);
也给你0,System.out.println(“a_uuuuuub_uuuuc.trim().split(“.split”).length)
给你7@Miquel实际上trim()
甚至没有帮助s.trim().split().length==1
当s==“”
时,我必须检查“”
在条件中,您知道更好的方法吗?