开发者

increment date of data that filtered from the text file

开发者 https://www.devze.com 2023-02-01 19:35 出处:网络
public class Reader { public static void main(String[] args) throws IOException, ParseException { BufferedReader reader;
public class Reader {
        public static void main(String[] args) throws IOException, ParseException {
            BufferedReader reader;
            String animalName="cat";
            String animal = null;
            try {
                reader = new BufferedReader(new InputStreamReader(
                        new FileInputStream("C:/dila.txt")));
                Map<String, Integer> result = new LinkedHashMap<String, Integer>();
                Map<String, Integer> result2 = new LinkedHashMap<String, Integer>();

                while (reader.ready()) {
                    String line = reader.readLine();
/split a line with spaces/
                    String[] values = line.split(",");
                    String key = null;                 
                    if(values[1].compareTo(animalName)==0){
                    key = values[0];
                    animal=""+values[1].compareTo(animalName);
                    int sum = 0;
                    int count = 0;
/get a last counter and sum/
                    if (result.containsKey(key)) {
                        sum = result.get(key);
                        count = result2.get(key);
                    } else{
                    }
 /increment sum a count and save in the map with key/
                    result.put(key, sum + Integer.parseInt(values[2]));
                    result2.put(key, count + 1);
                }
                }

 /interate and print new output/
                for (String key : result.keySet()) {
                    Integer sum = result.get(key);
                    Integer count = result2.get(key);
                    System.out.println(key +"   "+animalName+ " " + sum + "\t" + count);
                }
                reader.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block开发者_JAVA技巧
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
  • i have below text file

    11/2/2010,cat,6

    11/2/2010,cat,3

    11/2/2010,dog,4

    11/2/2010,cat,11

    11/3/2010,cat,1

    11/3/2010,dog,3

    11/3/2010,cat,8

    11/3/2010,cat,80

  • in my java code it is filtered below output.

    11/2/2010    cat    20    3

    11/3/2010    cat    104    4

    11/4/2010    cat    26    2

  • But i want below mention result.

    11/01/2010

    11/02/2010    cat    20    3

    11/03/2010    cat    104    4

    11/04/2010    cat    26    2

    11/05/2010

    11/06/2010

    11/07/2010

    11/08/2010

    11/09/2010

    11/10/2010

    11/11/2010

    11/12/2010

    11/13/2010

    11/14/2010

    11/15/2010

    11/16/2010

    11/17/2010

    11/18/2010

    11/19/2010

    11/20/2010

    11/21/2010

    11/22/2010

    11/23/2010

    11/24/2010

    11/25/2010

    11/26/2010

    11/27/2010

    11/28/2010

    11/29/2010

    11/30/2010

    Above shows my java code...(i am new for this site thats why my question is little bit unclear to you. now i got the method. Now i again post my question. So please read this and give me the solution....)


Do this:

  1. read the first line of txt file to get the month.
  2. create a hashmap of size of the month. Populate it with java.util.date from 1st to the last of the dates in that month. Date as key and null as value.
  3. read the text file, get the first of CSV in each line convert it to Date object
  4. Do a lookup in Hashmap and insert the fomatted String as the value to corresponding date key. You can use Formatter for padding.
  5. repeat #3 and #4 till EOF
  6. Now, iterate in Hashmap and print out. For all the NULL values just print formatted Key (which is date)

Hope this helps.

 1. use SimpleDateFormat to convert date String to date object
 2. write a method that takes this date and returns maximum days in that month using java.util.Calendar.getMaximum
 3. create a hashMap with Keys as Dates in that month and values as Null
 4. now, read line by line and user SimpleDateFormat to convert first of CSV into date. Use this date as key and put the printable String as value.
 5. Iterate throught this hashmap.keys() and to get the values one by one. If the value is null just print the key i.e. date.

:)

Can't help more.


Hope this method helps you in filling the keys.

public static String[] getDates(String date)
    {
        String[] dates=null;

        SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");

        try {
            Date d=sdf.parse(date);

            Calendar cal=GregorianCalendar.getInstance();

            cal.setTime(d);

            int month=cal.get(Calendar.MONTH);
            int year=cal.get(Calendar.YEAR);
            int startDate=cal.getActualMinimum(Calendar.DAY_OF_MONTH);
            int endDate=cal.getActualMaximum(Calendar.DAY_OF_MONTH);

            StringBuilder sb=new StringBuilder();

            dates=new String[endDate];

            for(int i=startDate;i<=endDate;i++)
            {
              sb=new StringBuilder();
              sb.append(month+1);
              sb.append("/");
              sb.append(i);
              sb.append("/");
              sb.append(year);

              dates[i-1]=sb.toString();

            }   

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return dates;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号