Java:我的split方法有问题

时间:2014-06-02 05:12:11

标签: java arrays if-statement for-loop lexer

我正在尝试创建一个split方法,将从输入文件读入的行拆分为我返回的字符串数组。问题是当我通过for循环发送诸如“ho; hi hello”之类的行时,它总是在第一个分隔符或";"或第一个运算符(如"+")之后停止。我不知道为什么它会这样做,因为我希望它继续迭代直到行尾,这意味着在if语句再次迭代for循环直到i < line.length()

If String line =="ho;hi hello" 

我想打印出来

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

目前只打印

This is the string in location 0: ho
This is the string in location 1: ;

始终在第一个分隔符或运算符处停止。这是split方法的代码。

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;
  }

2 个答案:

答案 0 :(得分:1)

我在这里发现了3个问题。

  1. SPACE不是分隔符,因此预期输出为:

    This is the string in location 0: ho
    This is the string in location 1: ;
    This is the string in location 2: hi hello
    
  2. 在向其中添加任何新字符之前,您需要重置temp。更好地使用StringBufferStringBuilder

  3. 您还需要将最后temp添加到数组中。

  4. 我在这里提供修改后的for循环:

    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);
    

答案 1 :(得分:0)

你不包括&#39; &#39;作为分隔符。此外,您需要将temp重置为&#34;&#34;将它添加到数组后。

相关问题