我是新來的java,所以很抱歉,如果這是一個明顯的問題。如何閱讀在字符串中的字符在java中
我試圖按字符讀取字符串來創建樹節點。 例如,輸入"HJIOADH"
和節點都H J I O A D H
我注意到,
char node = reader.next().charAt(0); I can get the first char H by this
char node = reader.next().charAt(1); I can get the second char J by this
我可以使用一個週期來獲取所有的人物?像
for i to n
node = reader.next().charAt(i)
我試過了,但它不起作用。
我該怎麼做?
非常感謝您的幫助。
Scanner reader = new Scanner(System.in); System.out.println(「輸入您的節點爲大寫字母無空格,'/'結尾」); int i = 0; char node = reader.next()。charAt(i); 而(節點!=「/」){
CreateNode(node); // this is a function to create a tree node
i++;
node = reader.next().charAt(i);
}
安置自己的完整的代碼,因爲我們不知道讀者? – brso05 2014-10-10 18:52:39
使用'char [] array =「HJIOADH」.toCharArray();獲取數組中的字符''探索'String'類中存在的方法本身。 – Braj 2014-10-10 18:52:39
謝謝大家!我知道了。 – Ahaha 2014-10-10 19:10:24