文本中有如下数据:
  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大家有什么好的方法?

解决方案 »

  1.   

    例子 不够清楚 有没有可能 1.3的时候 一个是a1 一个是a2 如果可能 输出什么?a3?
    还有就是1.5的是一个是a 一个是b 最后2列 都肯定是想通的 ?
      

  2.   


    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
      

  3.   


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

  4.   

    我也是用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
    输出顺序好像反了,哈哈
      

  5.   


    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]);
    }
    }}
      

  6.   

    用TreeMap或者SortMap就可以了