我想要做的基本上是这样的:
>读一个文件;
>删除所有标点符号并将所有字母转换为小写;
>将单词转换为4个字母短语(如果单词短于4个字符,则将其作为一个整体);
例:
Input: Hello,my identification is Mister Dude.
Output: hell,ello,my,iden,dent,enti,ntif,tifi,ific,fica,icat,cati,atio,tion,is,mist,iste,ster,dude.
如果我能将每个4字短语作为数组中的单独值,那将是很好的.
现在我设法完成的事情:
public String[] OpenFile() throws IOException { FileReader fr = new FileReader(path); BufferedReader textReader = new BufferedReader(fr); int numberOfLines = readLines(); String[] textData = new String[numberOfLines]; int i; for (i = 0; i < numberOfLines; i++) { textData[i] = textReader.readLine(); textData[i] = textData[i].replaceAll("[^A-Za-ząčęėįšųūž]+"," ").toLowerCase(); } textReader.close(); return textData; }
textData [i]是我需要划分的每行文本.
我已经尝试了几种方法,例如.tochararray和2D数组,但我似乎无法管理字母排列部分.我怎样才能完成第3项任务?
解决方法
测试
ideone.com:
public static void main (String[] args) { String text = "Hello,my identification is Mister Dude."; String[] words = text.replaceAll("[^(\\w )]+","").toLowerCase().split(" "); for (String word : words) { if (word.length() <= 4) { System.out.println(word); } else { for (int i = 0; i <= word.length() - 4; i++) { System.out.println(word.substring(i,i + 4)); } } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。