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

1. Senior Member
Join Date
May 2010
Posts
117
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## 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.

3. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
786
Rep Power
5

## 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. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

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

Ah.
Missed the sorting.

5. Senior Member
Join Date
May 2010
Posts
117
Rep Power
0

## 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. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
786
Rep Power
5

## 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. Senior Member
Join Date
May 2010
Posts
117
Rep Power
0

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

Originally Posted by eRaaaa
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

8. Senior Member
Join Date
May 2010
Posts
117
Rep Power
0

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

Originally Posted by renu
Hi,

Thank You
Hi,

When I run this code, it is giving me

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 {
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. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
786
Rep Power
5

## 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. Senior Member
Join Date
May 2010
Posts
117
Rep Power
0

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

Originally Posted by eRaaaa
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. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
786
Rep Power
5

## 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 !

12. Senior Member
Join Date
May 2010
Posts
117
Rep Power
0

## 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. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
786
Rep Power
5

## 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. Senior Member
Join Date
May 2010
Posts
117
Rep Power
0

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

Thank You Sir. Learnt a lot today.

#### Posting Permissions

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