我编写了一个程序,给出了所有内容的列表,并在其周围加上了单引号,并在末尾加了句号,如
“狗很酷”变成了“狗”,“是”,“酷”
除非问题是程序在单引号字符中添加一行
这是结果
'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]: https://i.stack.imgur.com/WXBKs.png
答案 0 :(得分:0)
因此,在没有输入的情况下很难说清楚,但是在输入中似乎还有其他空白,例如回车,这会导致您的解析失败。此外,如果事物具有多个空格或以空格结尾,则可能会得到比您想要的更多的值(例如,尾部逗号,我认为您会避免这样做)。您原来的例程适用于“狗很酷”,但不适用于“狗\ rare \ rcool \ r”。我认为这是一个经过稍微修改的版本,可以解决问题(我还拉出了未使用的jframe参数)。 我也试着将其视为逗号,除了第一个以外的任何单词都在逗号之前。我为此引入了一个布尔值,尽管它可以检查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;
}