java:给定一个字符串,在其中选择数字,按升序排序,然后打印结果

时间:2015-12-18 21:28:42

标签: java java.util.scanner

正如标题所示。例如,如果输入为9103ndhai*25ma@#$,则程序应输出012359。 这是我的尝试:

public static void main (String args[]) {
    Scanner cin = new Scanner (System.in);
    String myString = "";
    while (cin.hasNext()) {
        int tempInt = cin.nextInt();
        myString = myString + tempInt;
    }

    Scanner intScan = new Scanner (myString);
    List<Integer> intList = new ArrayList();
    while (intScan.hasNext()) {
        int tempInt2 = intScan.nextInt();
        intList.add(tempInt2);
    }

    Collections.sort (intList);
    for (int i=0; i<intList.size(); i++){
        System.out.println(intList.get(i));
    }
}

这会产生错误的结果。我特别想使用Scanner,但我认为我对课程有一些误解(会更接近api)。有什么建议吗?

2 个答案:

答案 0 :(得分:-1)

我详细说明了你的代码并尝试使用它:

FOREACH (GROUP A by field1) {
        field_a = FILTER A by field2 == TRUE;
        field_b = FILTER A by field4 == 'some_value' AND field5 == FALSE;
        field_c = DISTINCT field_b.field3;

        GENERATE  FLATTEN(group),
                  COUNT(field_a) as fa,
                  COUNT(field_b) as fb,
                  COUNT(field_c) as fc,

答案 1 :(得分:-1)

这应该有效

public static void main(String args[]){
    Scanner scanner = new Scanner(System.in);

    String input = scanner.next();

    Pattern p = Pattern.compile("\\d"); //Matches single digits (from 0-9) - prepend -? to the regex to react to "negative" digits
    Matcher m = p.matcher(input);
    List<Integer> integers = new ArrayList<Integer>();
    while (m.find()) {
        integers.add(Integer.parseInt(m.group()));
    }
    Collections.sort(integers);

    integers.forEach(System.out::print);
}