在 split 命令之后打印出来的单词不是按升序打印出来的。我认为我没有将它放置在代码的正确位置,但我不确定到底要放置它。它根据需要逐字打印整个文本段落,没有任何标点符号,但不会按升序(字母)顺序打印。任何帮助都会很棒。
public static void main(String[] args) throws
FileNotFoundException, IOException
{
Scanner ci = new Scanner(System.in);
System.out.print("Please enter a text file to open: ");
String filename = ci.next();
System.out.println("");
File file = new File(filename);
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String str;
while((str = br.readLine())!= null)
{
String sp[] = str.split("[\\s\\.,;:\\?!]+");
for (String sr : sp )
{
System.out.println(sr);
}
sb.append(str);
sb.append(" ");
// System.out.println(str);
}
ArrayList<String> text = new ArrayList<>();
StringTokenizer st = new StringTokenizer(sb.toString().toLowerCase());
while(st.hasMoreTokens())
{
String s = st.nextToken();
text.add(s);
}
System.out.println("\n" + "Words Printed out in Ascending "
+ "(alphabetical) order: " + "\n");
List<String> arrayList = new ArrayList<>(text);
Collections.sort(arrayList);
for (Object ob : arrayList)
{
System.out.println("\t" + ob.toString());
}
}