Results 1 to 14 of 14
  1. #1
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question How to count the total records for each unique first 2 characters in an Input File .

    Hi

    I have an input file, for example like this
    09DR07858UFORCIER
    66SY73056NPEMDAY
    02DR07858UFORCIER
    01SY73056NPEMDAY
    02DR07858UFORCIER
    03SY73056NPEMDAY
    01DR07858UFORCIER
    04SY73056NPEMDAY
    09DR07858UFORCIER
    66SY73056NPEMDAY

    Based on the first 2 characters, for each unique value I have to give a count of records for it.
    From the above Input file, My Output should look like
    01,2
    02,2
    03,1
    04,1
    09,2
    66,2

    The first one is the unique number from the input file(first 2 characters, sorted in ascending order) and a comma after it. Followed by the count of records for that unique number.
    All this should be in ascending order.

    I tried using the substring method, to get the count. But the problem with it is, I dont know which number exists in the first 2 characters of the input file.
    Is their any other way to do it.

    Thank You.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    HashMap<String, Integer>
    As you read each line, grab the first two characters (you're already doing that by the sounds of it), then find in the Map.
    If it exists then increment the value, if not then put the new id in the Map along with a value of 1.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    Instead of using a HashMap I would suggest to use a TreeMap (because of the requirement of sorting)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    Ah.
    Missed the sorting.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    Thank U.

    Can You Please show me how to increment the value.Whether it should be inside the put statement.

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    e.g.
    Java Code:
    				String value = line.substring(0, 2);
    				Integer count = map.get(value);
    				map.put(value, count==null?1:++count);

  7. #7
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question Re: How to count the total records for each unique first 2 characters in an Input Fil

    Quote Originally Posted by eRaaaa View Post
    e.g.
    Java Code:
    				String value = line.substring(0, 2);
    				Integer count = map.get(value);
    				map.put(value, count==null?1:++count);
    Hi,

    Thank You for the reply.

    Thank You

  8. #8
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    Quote Originally Posted by renu View Post
    Hi,

    Thank You for the reply.

    Thank You
    Hi,

    When I run this code, it is giving me
    Exception in thread "main" java.lang.NullPointerException

    and the line it is pointing too is,
    int count = map.get(value);

    I am understanding it , as : Since their is no value in the treemap it is gving that error.

    These are my lines of code:
    try {
    readin = new BufferedReader(new FileReader(InFile));
    for(String input ;(input = readin.readLine()) != null; ){
    inCount++;
    String mainStr = input.substring(0, 500);
    writeOutFile1.write(mainStr);
    writeOutFile1.newLine();
    value = input.substring(0,2);
    System.out.println("VALUE: "+value);
    int count = map.get(value);
    map.put(value, count == 0?1:++count);

    }


    Any help is greatly apprciated.Pls

  9. #9
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    int count = map.get(value);
    !=
    Integer count = map.get(value);

  10. #10
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    Quote Originally Posted by eRaaaa View Post
    int count = map.get(value);
    !=
    Integer count = map.get(value);
    The IDE i use, takes the datatype as int not Integer.
    int count = 0;
    is correct in my IDE.

    Sir,
    How will it get the value before even entering into the map for the first time. Thats why it is saying NullPointerException.
    How to avoid that, I am not knowing.

    Theoritically, bring the first 2 characters. Find in the Map(first time, the map doesnot contain anything, since no value is put in it).How to avoid the NullPointerException ,when checking for the first time

  11. #11
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    Ok that`s my last post here:

    How to avoid the NullPointerException
    I write it again: Use Integer instead of int :)

    The reason why do you will get a NPE if you are using int, is because map.get(..) returns an object (Integer!). If you declare count as an int instead of Integer, the compiler will change the map.get(..) to map.get(..).intValue().
    If map.get(..) returns a null value, because there is no entry in the map with that key, you will invoke the inValue() method onto a null reference --> NullPointerException. If you use Integer, the count value holds a reference to a null vlaue -> which you can check with count == null -> and not == 0 like in your code !
    I will hope that this will help you.

  12. #12
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    Thank You So Much Sir,

    For all you have done to me today. Thank You for the explanation.

    It works.

    For anyone having to do the same work, here is the code to extra the data
    for(String key : map.keySet()){
    System.out.println("Result: " + key + "," + map.get(key));
    }

    With your efforts, I could finish this project.Thank You

  13. #13
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    If you need both (value and key) it would be better if you would use the entrySet method instead of iterating over the keyset and using the get method !

    Java Code:
    		for (Entry<String, Integer> entry : map.entrySet()) {
    			System.out.println("Result: " + entry.getKey() + "," + entry.getValue());
    		}

  14. #14
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default Re: How to count the total records for each unique first 2 characters in an Input Fil

    Thank You Sir. Learnt a lot today.

Similar Threads

  1. How to count records WITHOUT an array?
    By teekei in forum New To Java
    Replies: 17
    Last Post: 07-25-2011, 01:42 PM
  2. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  3. How to count total items of JCheckbox
    By khb3283 in forum AWT / Swing
    Replies: 3
    Last Post: 03-13-2009, 12:31 PM
  4. Count the characters in an input box.
    By TheRocket in forum New To Java
    Replies: 3
    Last Post: 11-20-2008, 02:25 PM
  5. How to remove Control Characters from an input file?
    By renjan in forum Advanced Java
    Replies: 0
    Last Post: 08-01-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •