gpt4 book ai didi

java - 格式化 JTextArea 的结果,使其不跳过行?

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

我编写了一个程序,给定一个任何内容的列表,在其周围添加单引号,并在末尾添加一个撇号,就像

“狗很酷”变成“狗”、"is"、“酷”

除了问题是程序为单引号字符提供一行

这是结果

'190619904419','
190619904469','
190619904569','
190619904669','
190619904759','
190619904859','
190619904869','
'

看看它如何将单引号附加到第一行的末尾当它应该是以下内容时

'190619904419',
'190619904469',
'190619904569',
'190619904669',
'190619904759',
'190619904859',
'190619904869',

文本在JTextArea中输入,我执行以下操作

字符串行= JTextArea.getText().toString()

我把它扔到这个方法中。

     private static String SQLFormatter(String list, JFrame frame){
String ret = "";
String currentWord = "";
for(int i = 0; i < list.length(); i++){
char c = list.charAt(i);

if( i == list.length() - 1){
currentWord += c;
currentWord = '\'' + currentWord + '\'';
ret += currentWord;
currentWord = "";
}
else if(c != ' '){
currentWord += c;
}else if(c == ' '){

currentWord = '\'' + currentWord + '\'' + ',';
ret += currentWord;
currentWord = "";
}
}


return ret;
}



Any advice, the bug is in there somewhere but im not sure if its the method or some jtextarea feature I am missing.

[JTEXT AREA RESULTS][1]


[1]: /image/WXBKs.png

最佳答案

因此,如果没有输入,就很难判断,但输入中似乎还有其他空格,例如回车符,这会影响您的解析。另外,如果该内容有多个空格或以空格结尾,您可能会得到比您想要的更多的内容(例如尾随逗号,我看到您努力避免这种情况)。您最初的例程适用于“狗很酷”,但不适用于“狗\稀有\rcool\r”。这是一个稍微修改过的版本,我认为它解决了问题(我还取出了未使用的 jframe 参数)。我还尝试将其视为逗号位于除第一个单词之外的任何单词之前。我为此引入了一个 boolean 值,尽管它可以检查 ret 是否为空。

public static String SQLFormatter(String list) {
String ret = "";
String currentWord = "";
boolean firstWord = true;
for (int i = 0; i < list.length(); i++) {
// note modified to prepend comma to words beyond first and treat any white space as separator
// but multiple whitespace is treated as if just one space
char c = list.charAt(i);
if (!Character.isWhitespace(c)) {
currentWord += c;
} else if (!currentWord.equals("")) {
currentWord = '\'' + currentWord + '\'';
if (firstWord) {
ret += currentWord;
firstWord = false;
} else {
ret = ret + ',' + currentWord;
}
currentWord = "";
}
}


return ret;
}

关于java - 格式化 JTextArea 的结果,使其不跳过行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57043178/

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