gpt4 book ai didi

java - 如何使用 Java 中的 stringreader 获取字符串中的下一个字符?

转载 作者:行者123 更新时间:2023-11-30 09:27:50 27 4
gpt4 key购买 nike

考虑这段代码:

public static void main (String[] args) {

String name = "(My name is Bob)(I like computers)"

StringReader s = new StringReader(name);

try {
// This is the for loop that I don't know
for () {
String result = "";
// Here the char has to be appended to the String result.
}
System.out.println("The string is: " + result);

} catch (Exception e) {
e.toString();
}
}

我正在寻找的是一个 for 循环,它首先查看当前位置的字符,如果该字符不是“)”,则将其附加到字符串。但是,还应将字符“)”附加到字符串中。在此示例中,输出应为:

字符串结果是:(我叫鲍勃)

最佳答案

下面是一个可行的解决方案。

import java.io.StringReader;

public class Re {
public static void main (String[] args) {
String name = "(My name is Bob)(I like computers)";

StringReader s = new StringReader(name);

try {
// This is the for loop that I don't know
String result = "";
int c = s.read();
for (;c!= ')';) {
result = result + (char)c;
// Here the char has to be appended to the String result.
c = s.read();
}
result = result + ')';
System.out.println("The string is: " + result);

} catch (Exception e) {
e.toString();
}

}
}

关于java - 如何使用 Java 中的 stringreader 获取字符串中的下一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14322345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com