根据java中值的序列的出现来分离int值?

时间:2016-03-02 12:09:36

标签: java int

我有一个整数值: 1299129912

我想将其存储为 12 12 12 在int v1,v2,v3; 即,当9909发生时,我们需要单独分离值。是否有可能在java中。如果是这样,请任何人帮助我。

这是我尝试的代码

    int l = 1299129912;

    Pattern p = Pattern.compile("99");

    Matcher m1 = p.matcher(l);       
   if (m1.matches()) {
        System.out.println("\n");
    }

 Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method matcher(CharSequence) in the type Pattern is not applicable for the arguments (int)

4 个答案:

答案 0 :(得分:0)

是的,在java中很有可能。只需将整数转换为字符串,然后用空格替换9909。

示例:

echo $this->htmlEscape($value)
echo $this->htmlEscape(htmlspecialchars($value))

答案 1 :(得分:0)

我认为您已将{8}的值作为String,因为1234990912349909Integer.MAX_VALUE更多。然后,您可以split将字符串String[]转换为parseInt,并使用单独的值执行任何操作。例如。在每个元素上调用String[] values = myIntString.split("9909"); for (String value: values) { int v = Integer.parseInt(value); }

{{1}}

答案 2 :(得分:0)

如果你知道你总是会有3个名为v1,v2和v3的整数,那么下面的方法就可以了:

String[] numbers = l.toString().split("99");
int v1 = Integer.parseInt(numbers[0]);
int v2 = Integer.parseInt(numbers[0]);
int v3 = Integer.parseInt(numbers[0]);

但是,如果您事先不知道,那么最好这样做:

String[] numbers = l.toString().split("99");
int[] v = new int[numbers.length];

for (int i = 0; i < numbers.length; i++)
    v[i] = Integer.parseInt(numbers[i]);

答案 3 :(得分:0)

我发现这是向您展示如何解决问题的最简单方法: 我对每个重要步骤都提出了明确的意见。请检查一下:

    int num = 1239012390;
    // Convert int into a string
    String str = String.valueOf(num);
    // What separates the values
    String toBremoved = "90";
    String str1 = "";
    // Declare a String array to store the final results
    String[] finalStrings = new String[2];
    // i will be used as an index
    int i = 0;
    do {
        // Finds and separates the first value into another string
        finalStrings[i] = str.substring(0, str.indexOf(toBremoved));
        // removes the first number from the original string
        str = str.replaceFirst(finalStrings[i], "");
        // Remove the next separating value
        str = str.replaceFirst(str.substring(str.indexOf(toBremoved), str.indexOf(toBremoved) + toBremoved.length()), "");
        // increments the index
        i++;
    } while (str.indexOf(toBremoved) > 0);  // keeps going for a new iteration if there is still a separating string on the original string

    // Printing the array of strings - just for testing
    System.out.println("String Array:");
    for (String finalString : finalStrings) {
        System.out.println(finalString);
    }

    // If you want to convert the values into ints you can do a standard for loop like this
    // Lets store the results into an int array
    int [] intResults = new int [finalStrings.length];
    for (int j = 0; j < intResults.length; j++) {
        intResults[j] = Integer.valueOf(finalStrings[j]);
    }

    // empty line to separate results
    System.out.println();

    // Printing the array of ints
    System.out.println("int Array:");
    for (int intResult : intResults) {
        System.out.println(intResult);
    }

或者以简化和更准确的方式: (你可以使用上面的例子,如果你需要了解如何做到这一点很长)

int num = 1239012390;
String [] numbers = String.valueOf(num).split("90");
int num1 = Integer.parseInt(numbers[0]);
int num2 = Integer.parseInt(numbers[1]);

System.out.println("1st -> " + num1);
System.out.println("2nd -> " + num2);