如何在java中读取字符串中的字符

Aha*_*aha 14 java string

我是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
Run Code Online (Sandbox Code Playgroud)

我可以用一个循环来获取所有角色吗?喜欢

for i to n
    node = reader.next().charAt(i)
Run Code Online (Sandbox Code Playgroud)

我试过但它不起作用.

我怎么想这样做?

非常感谢您的帮助.

扫描仪读卡器=新扫描仪(System.in); System.out.println("将节点输入为大写字母,不加空格,最后输入'/'); int i = 0; char node = reader.next().charAt(i); while(node!='/'){

        CreateNode(node); // this is a function to create a tree node
        i++;
        node = reader.next().charAt(i);

    }
Run Code Online (Sandbox Code Playgroud)

cor*_*iKa 15

你只想给next()你的读者一次,除非它有很多相同的toke一次又一次地重复.

String nodes = reader.next();
for(int i = 0; i < nodes.length(); i++) {
    System.out.println(nodes.charAt(i));
}
Run Code Online (Sandbox Code Playgroud)