数据筛选问题

plplum 2010-04-13 10:22:53
文本中有如下数据:
1 1.1 4 a a1
2 1.2 4 a a1
2 1.3 6 a a2
2 1.2 5 a a1
3 1.3 9 a a2
3 1.5 6 b a1
3 1.1 3 a a1

读该文本中的数据,在控制台输出以下内容
(规律根据输出数据很容易总结,不再描述):
1 1.1 4 a a1
2 1.3 15 a a2
3 1.5 18 b a1

大家有什么好的方法?
...全文
93 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangqianjiao 2010-04-15
  • 打赏
  • 举报
回复
存到数据库里再取出来
wakeUpDoNottLazy 2010-04-13
  • 打赏
  • 举报
回复
没看出规律
plplum 2010-04-13
  • 打赏
  • 举报
回复

我是把相同的行放在map中处理的,你这种更简单

[Quote=引用 2 楼 goldenfish1919 的回复:]
Java code

import java.io.*;
public class Test3{
public static void main(String args[])throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader

(Test3.class.g……
[/Quote]
若鱼1919 2010-04-13
  • 打赏
  • 举报
回复

import java.io.*;
public class Test3{
public static void main(String args[])throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader

(Test3.class.getResourceAsStream("test.txt")));

String thisIndex="";
String nextIndex="";
double thisPart1=0;
double nextPart1=0;
String thisPart3="";
String nextPart3="";
String thisPart4="";
String nextPart4="";

int sum=0;

String line=br.readLine();//读入第一行
String[] p = line.trim().split("\\s");
thisIndex=p[0];
thisPart1=Double.parseDouble(p[1]);
thisPart3=p[3];
thisPart4=p[4];

sum+=Integer.parseInt(p[2]);

while((line=br.readLine())!=null){ //读入下一行

String[] parts = line.trim().split(" ");

nextIndex=parts[0];
nextPart1=Double.parseDouble(parts[1]);
nextPart3=parts[3];
nextPart4=parts[4];

if(nextIndex.equals(thisIndex)){//作和

sum+=Integer.parseInt(parts[2]);

}else{//输出一条记录

System.out.println(thisIndex+" "+thisPart1+" "+sum+" "+thisPart3+"

"+thisPart4);
sum=Integer.parseInt(parts[2]);
}

thisIndex=nextIndex;

if(thisPart1<nextPart1){
thisPart1=nextPart1;
thisPart3=nextPart3;
thisPart4=nextPart4;
}
}

System.out.println(thisIndex+" "+thisPart1+" "+sum+" "+thisPart3+" "+thisPart4);

br.close();
}
}
test.txt:
1 1.1 4 a a1
2 1.2 4 a a1
2 1.3 6 a a2
2 1.2 5 a a1
3 1.3 9 a a2
3 1.5 6 b a1
3 1.1 3 a a1
LuffySY 2010-04-13
  • 打赏
  • 举报
回复
例子 不够清楚 有没有可能 1.3的时候 一个是a1 一个是a2 如果可能 输出什么?a3?
还有就是1.5的是一个是a 一个是b 最后2列 都肯定是想通的 ?
zoeg 2010-04-13
  • 打赏
  • 举报
回复
a1,a2什么规律?没看出来!!!!!!
前面的虽然隐约有点规律,但你要非说不是这样的规律,也不是不行...
描述都懒的打......................................
wangqianjiao 2010-04-13
  • 打赏
  • 举报
回复
有收获顶一下
plplum 2010-04-13
  • 打赏
  • 举报
回复
用TreeMap或者SortMap就可以了
[Quote=引用 5 楼 wangqianjiao 的回复:]
我也是用map做的,我感觉这个简单

Java code

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java……
[/Quote]
wangqianjiao 2010-04-13
  • 打赏
  • 举报
回复

package com.jp.file;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class ReadFile {
public static void main(String[] args) {
Map<String,String[]> datas=new HashMap<String,String[]>();
String[] data=null;
String[] tmp=null;
BufferedReader read=null;
try {
read=new BufferedReader(new FileReader("c:/ceshi.txt"));
String line="";
while(true){
line=read.readLine();
if(line==null) break;
data=line.split(" ");
if(datas.containsKey(data[0])){
tmp=datas.get(data[0]);
tmp[2]=Integer.parseInt(tmp[2])+Integer.parseInt(data[2])+"";
if(Double.parseDouble(data[1])>Double.parseDouble(tmp[1])){
tmp[1]=data[1];
tmp[4]=data[4];
tmp[3]=data[3];//刚才这里少了一行
}
}else{
datas.put(data[0], data);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(read!=null)
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//进行输出datas
Iterator<String[]> it=(Iterator) (datas.values().iterator());
while(it.hasNext()){
tmp=it.next();
System.out.println(tmp[0]+" "+tmp[1]+" "+tmp[2]+" "+tmp[3]+" "+tmp[4]);
}
}

}

wangqianjiao 2010-04-13
  • 打赏
  • 举报
回复
我也是用map做的,我感觉这个简单

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class ReadFile {
public static void main(String[] args) {
Map<String,String[]> datas=new HashMap<String,String[]>();
String[] data=null;
String[] tmp=null;
BufferedReader read=null;
try {
read=new BufferedReader(new FileReader("c:/ceshi.txt"));
String line="";
while(true){
line=read.readLine();
if(line==null) break;
data=line.split(" ");
if(datas.containsKey(data[0])){
tmp=datas.get(data[0]);
tmp[2]=Integer.parseInt(tmp[2])+Integer.parseInt(data[2])+"";
if(Double.parseDouble(data[1])>Double.parseDouble(tmp[1])){
tmp[1]=data[1];
tmp[4]=data[4];
}
}else{
datas.put(data[0], data);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(read!=null)
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//进行输出datas
Iterator<String[]> it=(Iterator) (datas.values().iterator());
while(it.hasNext()){
tmp=it.next();
System.out.println(tmp[0]+" "+tmp[1]+" "+tmp[2]+" "+tmp[3]+" "+tmp[4]);
}
}

}


输出结果:
3 1.5 18 a a1
2 1.3 15 a a2
1 1.1 4 a a1
输出顺序好像反了,哈哈

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧