分割命令之後打印出的字不是以升序打印出。我認爲我沒有把它放在代碼的正確位置,但我不確定是否放置它。它打印出整個文本段落的單詞,沒有任何標點符號,但不會按照升序(按字母順序)打印。任何幫助都會很棒。試圖找出爲什麼分割方法並不printg出右
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());
}
}
你確定數組是否被排序? –
你似乎先被分割在所有標點符號的字符串,然後把它粘到一起,然後再拆呢......任何理由再次粘合的陣列? – Kninnug
數組並得到整理...我真的不知道爲什麼我這樣做,我沒有與分裂 – user2208495