gpt4 book ai didi

java - 使用 isLetter() 将字符串拆分为单词,然后在新行打印出每个单词

转载 作者:行者123 更新时间:2023-12-02 00:12:14 25 4
gpt4 key购买 nike

input:
this is a line

output:
this
is
a
line

这个想法是用户输入一行,然后一行一个单词打印出来,然后继续。buffer 和 inputString 是 String 的变量。

    for (int i=0;i<inputString.length();i++){
if(Character.isLetter(inputString.charAt(i))){
buffer += i;
}// end if
}// end for i

用我很酷的错误消息结束这一切。

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The local variable buffer may not have been initialized

我对java编码很陌生,我知道还有很多其他方法可以解决这个任务,比如使用split()和其他一些方法。但我的施虐老师希望我使用 isLetter() 来实现这一点。

最佳答案

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The local variable buffer may not have been initialized

意味着你已经声明了一个变量(也许是一个字符串?)但没有初始化它。

替换

String buffer;

String buffer = ""; 

我不确定你的循环到底做了什么,将计数器添加到任何东西对我来说并没有真正意义,但那是另一个故事了。

关于java - 使用 isLetter() 将字符串拆分为单词,然后在新行打印出每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12543782/

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