文件out.txt中,有如下内容:
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-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-398114 141 411 699 969 996要求:
编写程序,从out.txt中读取内容,经过处理后,显示
效果如下:
015 049 059 139 149 239 247 248 259 349026 046 127 137 238 248 258 359 369 379
389114 699注:将数字外的其他字符去掉,然后把重复数字去掉,只保留最小的数字。最后将数字按从小到大的顺序排列,
每行10组数字。遇到空数字的行,输出时也要空行输出。
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-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-398114 141 411 699 969 996要求:
编写程序,从out.txt中读取内容,经过处理后,显示
效果如下:
015 049 059 139 149 239 247 248 259 349026 046 127 137 238 248 258 359 369 379
389114 699注:将数字外的其他字符去掉,然后把重复数字去掉,只保留最小的数字。最后将数字按从小到大的顺序排列,
每行10组数字。遇到空数字的行,输出时也要空行输出。
解决方案 »
- 谁能告诉我long型变量是怎样初始化的?
- eclipse编译错误为什么会显示?
- 静态块的执行时间与调用方式
- [求助] ScheduledThreadPoolExecutor
- 关于ATM自动取款的问题:我想返回上一级,而不是用这种方法,怎么做?
- 谁有jDK6.0.exe文件 电脑出了点问题 下载不了
- 刚下了eclipse3.2路过的帮忙请看看是不是因为版本不同的原因啊
- 菜鸟提问:键盘读取数值的问题
- 安装了JBUILDER8和TOMCAT4,.1如何在JBUILDER8中配置好TOMCAT?
- 100分:如何判断一个数是不是质数
- 请问这个程序怎么运行不成呢?请高手指点,谢谢!
- 请各位高手帮忙看看 GUI
package com.chinghoi.oy;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class Out
{
public static void main(String... args)
{
StringBuffer basicSB = new StringBuffer();
basicSB.append("015 ,049 051 059 /094 095 105 139 149 150 ");
basicSB.append("193 194 239 247 248 259 274 ?284 293 295 ");
basicSB.append("319 329 349 391 392 394 409 419 427 428 ");
basicSB.append("439 472 482 490 491 -493 501+ 509 510 529 ");
basicSB.append("590 592* 724 742 824 842、 905 913 914 ");
basicSB.append("923 925 931 932 934 941 943 十950 952 ");
basicSB.append("-712-713- 731-721-602-620-604-640-823-832-824-842-825-852-582-528-285-258 ");
basicSB.append("-359 -395-369-396-379-397-389-398 ");
basicSB.append("114 141 411 699 969 996 ");
//1、去除特殊字符
String filterStr = filterCharToNumbers(basicSB.toString());
System.out.println(filterStr);
//2、去除相同的信息
List<String> noSameStrs = delSameNumer(filterStr);
//3、进行排序
Collections.sort(noSameStrs);
System.out.println(noSameStrs.toString());
}
/**
* 去除特殊字符 (保留空格和数字)
* @param str 需要过滤的字符串
* @return 过滤后的字符串
*/
private static String filterCharToNumbers(String str)
{
StringBuffer sb = new StringBuffer();
char[] chars = str.toCharArray();
for(int i=0 ;i<chars.length;i++)
{
char tempChar = chars[i];
if(tempChar>='0'&&tempChar<='9'){
sb.append(tempChar);
}
else if(tempChar==' '){
sb.append(tempChar);
}else{
sb.append(' ');
}
}
return sb.toString();
}
/**
* 去除相同数字过滤
* @param str
* @return
*/
private static List<String> delSameNumer(String str){
String[] strs = str.split(" ");
List<String> strList = new ArrayList<String>();
for(int i=0 ;i<strs.length;i++){
String tempStr1 = strs[i];
if(null!=tempStr1&&!"".equals(tempStr1)&&!" ".equals(tempStr1))
{
boolean needSave = true;
for(String tempStr2:strList){
if(tempStr1.equals(tempStr2))
{
needSave = false;
}
}
if(needSave)
{
strList.add(tempStr1);
}
}
}
return strList;
}
}
StringBuffer basicSB = new StringBuffer();
basicSB.append("015 ,049 051 059 /094 095 105 139 149 150 ");
basicSB.append("193 194 239 247 248 259 274 ?284 293 295 ");
basicSB.append("319 329 349 391 392 394 409 419 427 428 ");
basicSB.append("439 472 482 490 491 -493 501+ 509 510 529 ");
basicSB.append("590 592* 724 742 824 842、 905 913 914 ");
basicSB.append("923 925 931 932 934 941 943 十950 952 ");
basicSB.append("-712-713- 731-721-602-620-604-640-823-832-824-842-825-852-582-528-285-258 ");
basicSB.append("-359 -395-369-396-379-397-389-398 ");
basicSB.append("114 141 411 699 969 996 ");
然后每隔三位取一个字符串,然后放进Set集合里面,这样就得到唯一的.
文件是变化的话,你就用了监听的,定时监听文件的 md5是否一样就行了
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-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-398114 141 411 699 969 996要求:
编写程序,从out.txt中读取内容,经过处理后,显示
效果如下:
015 049 059 139 149 239 247 248 259 349026 046 127 137 238 248 258 359 369 379
389114 699注:将数字外的其他字符去掉,然后把重复数字去掉,只保留最小的数字。最后将数字按从小到大的顺序排列,如:114 141 411 输出为:114
每行10组数字。遇到空数字的行,输出时也要空行输出。
你只要写一下主方法调用用numsTest 并传递交的文件路径就可以了。如果得了来的结果不是你想要的,自己根据代码做相应的调整。个人觉得代码写得还算清晰就不加注释了。如疑问可加我QQ:402279001 . 下面是得出的结果
015 049 059 114 139 149 239 247 248 258
259 349 359 369 379 389 602 604 699 712
713 823private void numsTest(String fileName)throws IOException{
File file = new File(fileName);
BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
List<String> allNums = new ArrayList<String>();
while((line = bufReader.readLine())!=null){
if(line.trim().equals("")){
continue;
}
List<String> lineNums = getNums(line);
if(lineNums.size()==0){
continue;
}
allNums.addAll(lineNums);
}
Map<KeyClass,String> allNumsMap = filterNums(allNums);
List<String> allNum = new ArrayList<String>(allNumsMap.values());
Collections.sort(allNum);
printAllNum(allNum);
}
private void printAllNum(List<String> allNum){
StringBuffer sb = new StringBuffer();
for(int i =0;i<allNum.size();i++){
sb.append(allNum.get(i)).append("\t");
if((i+1)%10==0){
sb.append("\n");
}
}
System.out.println(sb);
}
private Pattern pattern = Pattern.compile("\\d+");
private List<String> getNums(String source){
Matcher match = pattern.matcher(source);
Set<String> numSet = new HashSet<String>();
while(match.find()){
String value = match.group();
numSet.add(value);
}
if(numSet.size()==0){
return new ArrayList<String>();
}
List<String> numList = Arrays.asList(numSet.toArray(new String[]{}));
return numList;
}
private Map<KeyClass,String> filterNums(List<String> nums){
Map<KeyClass,String> resourceMap = new HashMap<KeyClass,String>();
for(String numStr : nums){
KeyClass key = new KeyClass(numStr);
Integer num = Integer.valueOf(numStr);
String tempValue = resourceMap.get(key);
if(tempValue!=null){
Integer tempNum = Integer.valueOf(tempValue);
if(num<tempNum){
resourceMap.put(key, numStr);
}
}else{
resourceMap.put(key, numStr);
}
}
return resourceMap;
}
private static class KeyClass implements Comparable<KeyClass> {
private String num;
public KeyClass(String num){
this.num = num;
}
public int compareTo(KeyClass other){
Integer thisNum = Integer.valueOf(this.num);
Integer otherNum = Integer.valueOf(other.num);
return thisNum - otherNum;
}
@Override
public boolean equals(Object other){
if(other==null){
return false;
}
if(other instanceof KeyClass){
KeyClass otherKey = (KeyClass)other;
if(otherKey.num.equals(this.num)){
return true;
}
if(otherKey.num.length()!=this.num.length()){
return false;
}
char [] thisChars = this.num.toCharArray();
char [] otherChars = otherKey.num.toCharArray();
int equalsCount = this.num.length();
for(int i=0;i<thisChars.length;i++){
for(int j=0;j<otherChars.length;j++){
if(thisChars[i] == otherChars[j]){
otherChars[j] = 'a'; //这个a没有特别含义只是改变已经匹配上了的值
equalsCount--;
}
}
}
if(equalsCount==0){
return true;
}
}
return false;
}
@Override
public int hashCode(){
return 0;
}
}
要求:
编写程序,从out.txt中读取内容,经过处理后,显示
效果如下:
015 049 059 139 149 239 247 248 259 349026 046 127 137 238 248 258 359 369 379
389114 699
public static void main(String[] args) throws Exception{
File file=new File("C:/a.txt");
InputStreamReader reader=new InputStreamReader(new FileInputStream(file));
int size=1024;
char[] cbuf=new char[size];
StringBuilder sb=new StringBuilder();
for(int len=reader.read(cbuf);len>0;len=reader.read(cbuf)){
sb.append(cbuf,0,len);
}
reader.close();
String str=sb.toString();
List<String> pList=new ArrayList<String>();
List<String> bList=new ArrayList<String>();
Pattern bPat=Pattern.compile("\\n\\s+");
Matcher mcr=bPat.matcher(str);
int st=0,ed;
while(mcr.find()){
ed=mcr.start();
pList.add(str.substring(st, ed));
st=mcr.end();
bList.add(mcr.group());
}
pList.add(str.substring(st));
List<String> sList;
String s;
Pattern nPat=Pattern.compile("[0-9]{3}");
sb=new StringBuilder();
char[] cc;
int bs=bList.size(),row=0,col;
for(String p:pList){
sList=new ArrayList<String>();
mcr=nPat.matcher(p);
LP:
while(mcr.find()){
cc=mcr.group().toCharArray();
int[] nn=new int[3];
nn[0]=Integer.parseInt(String.valueOf(cc[0]));
nn[1]=Integer.parseInt(String.valueOf(cc[1]));
nn[2]=Integer.parseInt(String.valueOf(cc[2]));
Arrays.sort(nn);
s=""+nn[0]+nn[1]+nn[2];
for(String _s:sList){
if(_s.equals(s)){
continue LP;
}
}
sList.add(s);
}
Collections.sort(sList);
col=1;
for(String _s:sList){
if(col==0){
sb.append("\n");
col=1;
}
if(col++==10){
col=0;
}
sb.append(_s+" ");
}
if(row<bs){
sb.append(bList.get(row++));
}
}
str=sb.toString();
str=str.replaceAll("(?m) $","");
System.out.println(str);
}哇 终于写完了 吃晚饭了
如下代码234 244 272 632 642
935 944 974 975 450;452;453;454;455;456;457;459;361;362。
:364;367;368;183;273;094;274。正确输出效果:
227 234 236 244 246 359 449 479 579045 049 136 138 236 237 245 247 345 346
367 368 445 455 456 457 459你的输出效果:
227 234 236 244 246
359 449 479 579
045 136 236 245 345 445 455 456 457 459
049 138 237 247 346 367 368
2.通过正则表达式去掉特殊字符和重复字符,然后排序.
比如,String的replaceALL。自己再调试一下吧!
如下代码:450;452;453;454;455;456;457;459;361;362。
:364;367;368;183;273;094;274。
234 244 272 632 642
935 944 974 975 正确的输出效果:045 049 136 138 236 237 245 247 345 346
367 368 445 455 456 457 459227 234 236 244 246 359 449 479 579你的程序输出效果:
045 136 236 245 345 445 455 456 457 459
049 138 237 247 346 367 368
227 234 236 244 246
359 449 479 579
改为
Pattern bPat=Pattern.compile("\\n\\s+\\r?\\n");
现在只认回车换行 前面的空格不算了 空格加回车换行也算是回车换行啦
InputStreamReader reader=new InputStreamReader(new FileInputStream(file));
int size=1024;
char[] cbuf=new char[size];
StringBuilder sb=new StringBuilder();
for(int len=reader.read(cbuf);len>0;len=reader.read(cbuf)){
sb.append(cbuf,0,len);
}
reader.close();
String str=sb.toString();
List<String> pList=new ArrayList<String>();
List<String> bList=new ArrayList<String>();
Pattern bPat=Pattern.compile("\\n\\s+\\r?\\n");
Matcher mcr=bPat.matcher(str);
int st=0,ed;
while(mcr.find()){
ed=mcr.start();
pList.add(str.substring(st, ed));
st=mcr.end();
bList.add(mcr.group());
}
pList.add(str.substring(st));
List<String> sList;
String s;
Pattern nPat=Pattern.compile("[0-9]{3}");
sb=new StringBuilder();
char[] cc;
int bs=bList.size(),row=0,col;
for(String p:pList){
sList=new ArrayList<String>();
mcr=nPat.matcher(p);
while(mcr.find()){
cc=mcr.group().toCharArray();
int[] nn=new int[3];
nn[0]=Integer.parseInt(String.valueOf(cc[0]));
nn[1]=Integer.parseInt(String.valueOf(cc[1]));
nn[2]=Integer.parseInt(String.valueOf(cc[2]));
Arrays.sort(nn);
s=""+nn[0]+nn[1]+nn[2];
if(sList.contains(s)){
continue;
}
sList.add(s);
}
Collections.sort(sList);
col=1;
for(String _s:sList){
if(col==0){
sb.append("\n");
col=1;
}
if(col++==10){
col=0;
}
sb.append(_s+" ");
}
if(row<bs){
sb.append(bList.get(row++));
}
}
str=sb.toString();
str=str.replaceAll("(?m) $","");
System.out.println(str);
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws Throwable {
String filename = "out.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
String buf;
List<String> result = new ArrayList<String>();
Comparator<String> comp = 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*)+")) {
Collections.sort(result, comp);
print(result);
result.clear();
System.out.println();
} else {
for (String s : buf.replaceAll("^\\D+", "").split("\\D+")) {
char[] c1 = s.toCharArray();
Arrays.sort(c1);
boolean exist = false;
for (int i=0; i<result.size(); i++) {
String v = result.get(i);
char[] c2 = v.toCharArray();
Arrays.sort(c2);
if (Arrays.equals(c1, c2)) {
exist = true;
if (comp.compare(v, s) > 0) {
result.set(i, s);
}
break;
}
}
if (!exist) {
result.add(String.valueOf(c1));
}
}
}
}
if (result.size() > 0) {
Collections.sort(result, comp);
print(result);
}
br.close();
} public static void print(List<String> list) {
for(int i=0; i<list.size(); i++) {
if ((i+1)%10 == 0) {
System.out.println(list.get(i));
} else {
System.out.printf("%s ", list.get(i));
if (i == list.size()-1) {System.out.println();}
}
}
}
}
/*
测试数据
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-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-398114 141 411 699 969 996
*//*
打印结果
015 049 059 139 149 239 247 248 259 349026 046 127 137 238 248 258 359 369 379
389114 699
*/