Java的:有些事情不对我的拆分法 [英] Java: Something is wrong with my split method
问题描述
我想创建一个分裂的方式,分割线从输入文件中读入,我返回一个字符串数组。问题是,当我在线发送诸如豪;喜你好通过循环它总是第一个分隔符或后停止;
或先算这样的为+
。我不知道为什么它这样做,因为我想让它继续通过迭代,直到行的结束,后if语句再重复一次循环,直到意思 I< line.length()
如果串线==豪;喜你好
我想让它打印出来
这是位置0的字符串:HO
这是位置1字符串:;
这是位置2字符串:喜
这是位置3串:你好
目前它只是打印出
这是位置0的字符串:HO
这是位置1字符串:;
在第一个分隔符或操作员始终停止。这里是code分手的方法。
私有静态的String []斯普利特(串线){ 字符串TEMP =; // VAR存储从输入每一行暂时
布尔标志= FALSE;检查// VAR如果\\是从输入
的char [] = lineCharArray line.toCharArray(); //转化行成字符数组
清单<串GT;阵列=新的ArrayList<串GT;(); // ArrayList中存储分割字符串暂时 的for(int i = 0; I< line.length();我++){
如果(isDelimiter(lineCharArray [I])及!&放大器;!isOperator(lineCharArray [I])及&放大器;标志==假){
//System.out.println(lineCharArray[i]);
温度+ = lineCharArray [I]
}其他{
array.add(临时);
如果(isDelimiter(lineCharArray [I])){
array.add(将String.valueOf(lineCharArray [I]));
}
如果(isOperator(lineCharArray [I])){
array.add(将String.valueOf(lineCharArray [I]));
}
}
}
的String [] =串新的String [array.size()]; //即返回词法分析的字符串
array.toArray(字符串);
的for(int i = 0; I< strings.length;我++){
的System.out.println(这是在位置字符串+ I +:+串[I]);
}
返回字符串;
}
私有静态布尔isDelimiter(焦三){
炭[]定界符= {':',';','},{,[,],(,),,};
为(中间体X = 0; X&下; delimiters.length; X ++){
如果(C ==定界符[X])返回true;
}
返回false;
} 私有静态布尔isOperator(CHAR O){
烧焦[]运营= {'+',' - ','*','/','%','&所述;';!,&GT','=','和;',' |'};
为(中间体X = 0; X&下; operators.length; X ++){
如果(O ==操作符[X])返回true;
}
返回false;
}
我发现3个问题在这里。
-
SPACE并非如此预期的产出将是一个分隔符:
这是位置0的字符串:HO
这是位置1字符串:;
这是位置2字符串:你好喜 -
您需要添加任何新的char之前重置
温度
。更好地利用的StringBuffer
或的StringBuilder
。 -
您需要最后
温度
添加到阵列中也是如此。
下面我提供修改为
循环:
的for(int i = 0; I< line.length();我++){
如果(isDelimiter(lineCharArray [I])及!&放大器;!isOperator(lineCharArray [I])及&放大器;标志==假){
//System.out.println(lineCharArray[i]);
温度+ = lineCharArray [I]
}其他{
array.add(临时);
//重新设置临时变量。
TEMP =;
如果(isDelimiter(lineCharArray [I])){
array.add(将String.valueOf(lineCharArray [I]));
}
如果(isOperator(lineCharArray [I])){
array.add(将String.valueOf(lineCharArray [I]));
}
}
}
//最后临时添加到阵列。
array.add(临时);
I am trying to create a split method to split a line read in from an input file into an array of strings that I return. The problem is when I send in the line such as "ho;hi hello" through the for loop it always stops after the first delimiter or ";"
or the first operator such as "+"
. I'm not sure why it is doing that as I'd like for it to keep iterating through until the end of line, meaning after the if statements iterate once again the for loop until i < line.length()
If String line =="ho;hi hello"
I'd like for it to print out
This is the string in location 0: ho
This is the string in location 1: ;
This is the string in location 2: hi
This is the string in location 3: hello
Currently it is only printing out
This is the string in location 0: ho
This is the string in location 1: ;
Always stopping at the first delimiter or operator. Here is the code for the split method.
private static String[] split(String line) {
String temp = ""; //var to store each line from input temporarily
boolean flag = false; //var to check if "\" is in the from input
char[] lineCharArray = line.toCharArray(); //transforms line into array of chars
List<String> array = new ArrayList<String>(); //ArrayList to store the split strings temporarily
for (int i = 0; i<line.length(); i++){
if (!isDelimiter(lineCharArray[i]) && !isOperator(lineCharArray[i]) && flag==false){
//System.out.println(lineCharArray[i]);
temp += lineCharArray[i];
} else {
array.add(temp);
if (isDelimiter(lineCharArray[i])) {
array.add( String.valueOf(lineCharArray[i]));
}
if (isOperator(lineCharArray[i])) {
array.add( String.valueOf(lineCharArray[i]));
}
}
}
String [] strings = new String[array.size()]; //string that is returned for lexical analysis
array.toArray( strings );
for (int i = 0; i<strings.length; i++) {
System.out.println("This is the string in location " + i + ": " + strings[i]);
}
return strings;
}
private static boolean isDelimiter(char c) {
char [] delimiters = {':', ';', '}','{', '[',']','(',')',','};
for (int x=0; x<delimiters.length; x++) {
if (c == delimiters[x]) return true;
}
return false;
}
private static boolean isOperator(char o) {
char [] operators = {'+', '-', '*','/', '%','<','>','=','!','&','|'};
for (int x=0; x<operators.length; x++) {
if (o == operators[x]) return true;
}
return false;
}
I found 3 issues over here.
SPACE is not a delimiter so expected output will be:
This is the string in location 0: ho This is the string in location 1: ; This is the string in location 2: hi hello
You need to reset
temp
before adding any new char to it. Better useStringBuffer
orStringBuilder
.You need to add last
temp
to the array as well.
Here I am providing modified for
loop:
for (int i = 0; i<line.length(); i++){
if (!isDelimiter(lineCharArray[i]) && !isOperator(lineCharArray[i]) && flag==false){
//System.out.println(lineCharArray[i]);
temp += lineCharArray[i];
} else {
array.add(temp);
// Resetting temp variable.
temp = "";
if (isDelimiter(lineCharArray[i])) {
array.add( String.valueOf(lineCharArray[i]));
}
if (isOperator(lineCharArray[i])) {
array.add( String.valueOf(lineCharArray[i]));
}
}
}
// Adding last temp to the array.
array.add(temp);
这篇关于Java的:有些事情不对我的拆分法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!