文件out.txt中,有如下内容:411 414 439 441 491 493 511 539 551 911 913 914 919 931 934 941 943 953 961 991-712-713- 731-721-602-620-604-640-823-832-824-842-825-852-582-528-285-258
-359 -395-369-396-379-397-389-398016 061 106 156 160 165 345 354 369 396
435 453 457 475 479 497 516 534 543 547
561 574 601 610 615 639 651 678 679 687
693 697 745 749 754 768 769 786 794 796
867 876 936 947 963 967 974 976
114 141 411 699 969 996
015 ,049 051 059 /094 095 105 139 149 150
193 194 239 247 248 259 274 ?284 293 295
319 329 349 391 392 394 409 419 427 428
439 472 482 490 491 -493 501+ 509 510 529
590 592* 724 742 824 842、 905 913 914
923 925 931 932 934 941 943 十950 952要求:
编写程序,从out.txt中读取内容,经过处理后,显示
效果如下:
411 414 439 441 491 493 511 539 551 911
913 914 919 931 934 941 943 953 961 991258 285 359 369 379 389 395 396 397 398
528 582 602 604 620 640 712 713 721 731
823 824 825 832 842 852016 061 106 156 160 165 345 354 369 396
435 453 457 475 479 497 516 534 543 547
561 574 601 610 615 639 651 678 679 687
693 697 745 749 754 768 769 786 794 796
867 876 936 947 963 967 974 976015 049 051 059 094 095 105 114 139 141
149 150 193 194 239 247 248 259 274 284
293 295 319 329 349 391 392 394 409 411
419 427 428 439 472 482 490 491 493 501
509 510 529 590 592 699 724 742 824 842
905 913 914 923 925 931 932 934 941 943
950 952 969 996
-359 -395-369-396-379-397-389-398016 061 106 156 160 165 345 354 369 396
435 453 457 475 479 497 516 534 543 547
561 574 601 610 615 639 651 678 679 687
693 697 745 749 754 768 769 786 794 796
867 876 936 947 963 967 974 976
114 141 411 699 969 996
015 ,049 051 059 /094 095 105 139 149 150
193 194 239 247 248 259 274 ?284 293 295
319 329 349 391 392 394 409 419 427 428
439 472 482 490 491 -493 501+ 509 510 529
590 592* 724 742 824 842、 905 913 914
923 925 931 932 934 941 943 十950 952要求:
编写程序,从out.txt中读取内容,经过处理后,显示
效果如下:
411 414 439 441 491 493 511 539 551 911
913 914 919 931 934 941 943 953 961 991258 285 359 369 379 389 395 396 397 398
528 582 602 604 620 640 712 713 721 731
823 824 825 832 842 852016 061 106 156 160 165 345 354 369 396
435 453 457 475 479 497 516 534 543 547
561 574 601 610 615 639 651 678 679 687
693 697 745 749 754 768 769 786 794 796
867 876 936 947 963 967 974 976015 049 051 059 094 095 105 114 139 141
149 150 193 194 239 247 248 259 274 284
293 295 319 329 349 391 392 394 409 411
419 427 428 439 472 482 490 491 493 501
509 510 529 590 592 699 724 742 824 842
905 913 914 923 925 931 932 934 941 943
950 952 969 996
-359 -395-369-396-379-397-389-398016 061 106 156 160 165 345 354 369 396
435 453 457 475 479 497 516 534 543 547
561 574 601 610 615 639 651 678 679 687
693 697 745 749 754 768 769 786 794 796
867 876 936 947 963 967 974 976
114 141 411 699 969 996
015 ,049 051 059 /094 095 105 139 149 150
193 194 239 247 248 259 274 ?284 293 295
319 329 349 391 392 394 409 419 427 428
439 472 482 490 491 -493 501+ 509 510 529
590 592* 724 742 824 842、 905 913 914
923 925 931 932 934 941 943 十950 952要求:
编写程序,从out.txt中读取内容,经过处理后,显示
效果如下:
411 414 439 441 491 493 511 539 551 911
913 914 919 931 934 941 943 953 961 991258 285 359 369 379 389 395 396 397 398
528 582 602 604 620 640 712 713 721 731
823 824 825 832 842 852016 061 106 156 160 165 345 354 369 396
435 453 457 475 479 497 516 534 543 547
561 574 601 610 615 639 651 678 679 687
693 697 745 749 754 768 769 786 794 796
867 876 936 947 963 967 974 976015 049 051 059 094 095 105 114 139 141
149 150 193 194 239 247 248 259 274 284
293 295 319 329 349 391 392 394 409 411
419 427 428 439 472 482 490 491 493 501
509 510 529 590 592 699 724 742 824 842
905 913 914 923 925 931 932 934 941 943
950 952 969 996
注:将数字外的其他字符去掉,然后将数字按从小到大的顺序排列,
每行10组数字。遇到空数字的行,输出时也要空行输出。
2、将所有非数字字符替换成空格
3、用 String.split() 方法提取数字到字符串数组
4、用 Arrays.sort() 方法排序。
for example
import java.io.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws Throwable {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("out.txt")));
String buf;
List<String> list = new ArrayList<String>();
Comparator<String> com = new Comparator<String>() {
public int compare(String s1, String s2) {
return Integer.valueOf(s1) - Integer.valueOf(s2);
}
}; while ((buf=br.readLine()) != null) {
if (!buf.matches("(\\D*\\d+\\D*)+")) {
if (list.size() == 0) {continue;}
System.out.println();
Collections.sort(list, com);
for (int i=0; i<list.size(); i++) {
System.out.printf("%s ", list.get(i));
if ((i+1)%10 == 0) {System.out.println();}
}
list.clear();
System.out.println(); } else {
buf = buf.replaceAll("^\\D+(.*)", "$1");
list.addAll(Arrays.asList(buf.split("\\D+")));
}
}
if (list.size() > 0) {
System.out.println();
Collections.sort(list, com);
for (int i=0; i<list.size(); i++) {
System.out.printf("%s ", list.get(i));
if ((i+1)%10 == 0) {System.out.println();}
}
}
}
}