将输入字符串转换为系列

时间:2017-07-20 10:37:23

标签: java string

在下面的程序中,我试图检查input数组中的连续字符串是否在一个序列中,如果是这样,它们应该表示为一个范围,如下所示。 我能够将两个连续的字符串转换为范围,但我无法弄清楚如何检查和转换范围内的两个以上连续字符串。

    import java.util.*;
    public class HelloWorld{
    public static void main(String []args){
    System.out.println("Hello World");
    String input = "abc0001, abc0002, abc002, efg00113, efg00114, efg00115, rtasdf1";
    String[] result = input.split(", ");
    List<String> output = new ArrayList<String>();
    for(int i = 0; i < result.length - 1; ++i){
        String[] first = result[i].split("(?<=\\D)(?=\\d)");
        int digits1 = Integer.parseInt(first[1]);
        String[] second = result[i + 1].split("(?<=\\D)(?=\\d)");
        int digits2 = Integer.parseInt(second[1]);
        if(second[0].equals(first[0]) && (second[1].length() == first[1].length()) && ((digits2 - digits1) == 1)) {
            result[i] = result[i] + "-" + result[i + 1];
            output.add(result[i]);
            ++i;
        }
        else {
            output.add(result[i]);
        }
    }
    String [] out = output.toArray(new String[output.size()]);
    for(String a : out)
        System.out.println(a);

    //Arrays.sort(result);
    //for(String a : result)
    //    System.out.println(a);

    }
    }

输出:

    Hello World                                                                                                                                           
    abc0001-abc0002                                                                                                                                           
    abc002                                                                                                                                                    
    efg00113-efg00114                                                                                                                                        
    efg00115  

最后一个输出应该是efg00113-efg00115而不是efg00113-efg00114efg00115

2 个答案:

答案 0 :(得分:2)

检查一下。我在代码中标记了我更改的位置。我们的想法是记住我们必须转换多少连续字符串,并在找到不匹配的字符串后执行此操作。

编辑:,建议使用@daljindersingh编辑

import java.util.*;
public class HelloWorld{
public static void main( String[] args){
System.out.println( "Hello World");
String input = "test2, abc0001, abc0002, abc002, efg00113, efg00114, efg00115, rtasdf1, test123";
String[] result = input.split( ", ");
List<String> output = new ArrayList<String>();
int toMerge = 0; // <------>
for( int i = 0; i < result.length; ++i){ // <----------
    if( i == result.length - 1){
        if( toMerge != 0){
            result[ i] = result[ i - toMerge] + "-" + result[ i];
            output.add( result[ i]);
            toMerge = 0;
        }
        else{
            output.add( result[ i]);
        }
        break;
    } // ----------->
    String[] first = result[ i].split( "(?<=\\D)(?=\\d)");
    int digits1 = Integer.parseInt( first[ 1]);
    String[] second = result[ i + 1].split( "(?<=\\D)(?=\\d)");
    int digits2 = Integer.parseInt( second[ 1]);
    if( second[ 0].equals( first[ 0]) && (second[ 1].length() == first[ 1].length()) && ((digits2 - digits1) == 1)){
        toMerge++; // <--------
    }
    else{
        if( toMerge != 0){
            result[ i] = result[ i - toMerge] + "-" + result[ i];
            output.add( result[ i]);
            toMerge = 0;
        }
        else{
            output.add( result[ i]); // --------->
        }
    }
}
    String[] out = output.toArray( new String[ output.size()]);
    for( String a : out)
        System.out.println( a);

    // Arrays.sort(result);
    // for(String a : result)
    // System.out.println(a);

}
}

答案 1 :(得分:1)

您在代码中遇到了一些错误,特别是在某些运行中运行了2个索引,导致您无法在序列中添加第三个 - 并且只允许您检查对。请看下面的代码:

public static void main(String[] args) {
    System.out.println("Hello World");
    String input = "abc0001, abc0002, abc002, efg00113, efg00114, efg00115, rtasdf1";
    String[] result = input.split(", ");
    List<String> output = new ArrayList<String>();
    for(int i = 0; i < result.length - 1; ++i){
        String[] first = result[i].split("(?<=\\D)(?=\\d)");
        int digits1 = Integer.parseInt(first[1]);
        String[] second = result[i + 1].split("(?<=\\D)(?=\\d)");
        int digits2 = Integer.parseInt(second[1]);
        if(second[0].equals(first[0]) && (second[1].length() == first[1].length()) && ((digits2 - digits1) == 1)) {
            //Check if previous sequence contains the current. If so, replace
            if (!output.isEmpty() && output.get(output.size()-1).indexOf(result[i])>=0) {
              String startSequence = output.get(output.size()-1);
              if (startSequence.indexOf("-")>=0) {
                startSequence = startSequence.substring(0,startSequence.indexOf("-"));
              }

              output.remove(output.size()-1);
              result[i] = startSequence + "-" + result[i + 1];
            } else {
              result[i] = result[i] + "-" + result[i + 1];
            }
            output.add(result[i]);
        }
        else if (i>0) {
          output.add(result[i+1]);
        } else {
          output.add(result[i]);
        }
    }
    String [] out = output.toArray(new String[output.size()]);
    for(String a : out)
        System.out.println(a);

  }  

输出:

Hello World
abc0001-abc0002
abc002
efg00113-efg00115 
rtasdf1