I need to create a parent child relationship for the following string:
((OPERATING_CARRIER='AB' OR OPERATING_CARRIER='EY' OR (OPERATING_CARRIER='VA' AND (FLIGHT_NO=604 OR FLIGHT_NO=603))))
I have to insert them into a database table as following
ID PARENT_ID ENTITY OPERATOR VALUE
1 OPERATING_CARRIER = AB
2 OPERATING_CARRIER = EY
3 OPERATING_CARRIER = VA
4 3 FLIGHT_NO = 604
5 3 FLIGHT_NO = 603
using the following code
package whereclause;
import java.util.Iterator;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QueryMatcher {
public static void main(String[] args) {
// TODO Auto-generated method stub
String sa="((OPERATING_CARRIER='AB' OR OPERATING_CARRIER='AB' OR (OPERATING_CARRIER='VA' AND (FLIGHT_NO=604 OR FLIGHT_NO=603))))";
Matcher m = Pattern.compile("\\w+\\s*=\\s*(?:'[^']+'|\\d+)").matcher(sa);
System.out.println("contains "+sa.contains("((("));
Stack<String> in_cond = new Stack<String>();
Iterator<String> iter = in_cond.iterator();
String new_sa=sa;
while(m.find()) {
String aMatch = m.group();
// add aMatch to match list...
System.out.println(aMatch);
in_cond.push(aMatch);
}
System.out.println("string stack is "+in_cond);
int i=0;
for (String new_sa1:in_cond)
{
if(new_sa.contains(in_cond.get(i)))
{
new_sa=new_sa.replace(in_cond.get(i),"&"+i);
System.out.println("String Contains "+in_cond.get(i));
}
i++;
}
System.out.println("new String is "+new_sa);
}
}
i have got to the following output
contains false
OPERATING_CARRIER='AB'
OPERATING_CARRIER='AB'
OPERATING_CARRIER='VA'
FLIGHT_NO=604
FLIGHT_NO=603
string stack is [OPERATING_CARRIER='AB', OPERATING_CARRIER='AB', OPERATING_CARRIER='VA', FLIGHT_NO=604, FLIGHT_NO=603]
String Contains OPERATING_CARRIER='AB'
String Contains OPERATING_CARRIER='VA'
String Contains FLIGHT_NO=604
String Contains FLIGHT_NO=603
new String is ((&0 OR &0 OR (&2 AND (&3 OR &4))))
But now I am clueless on how to proceed, need help.
I have managed to solve it using following code for splitting the string
and to build the parent child relationship:
String input="name = 'name_1' AND in_stock IN {'in_stock_1','in_stock_2'} AND ( price BETWEEN '01-jan-2015' and '31-may-2015' OR price = 'price_3' )";
String sa =input;
String[] arr = sa.replaceAll("[()]+","").split("\\s*(\\sOR|\\sAND)\\s*");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
String og_st=orig_input;
Stack<String> temp_bool=new Stack<String>();
String[] bool_arr = og_st.split("\\s+");
String[] bool_op=new String[inout.length-1];
for(String bool:bool_arr)
{
if(bool.equals("AND") || bool.equals("OR"))
{
temp_bool.push(bool);
}
else
{
//nothing here
}
}
for (int i=0;i<temp_bool.size();i++)
{
bool_op[i]=temp_bool.get(i);
}
Conditions c=new Conditions();
String[] arr=null;
arr=inout;
//Stack<String> arr2 =new Stack<String>();
String[] atr=null;
if(arr[l].contains(" BETWEEN "))
{
atr=arr[l].split(" BETWEEN ");
c.id=l+1;
c.entity=atr[0];
c.operator=" BETWEEN ";
String c_value=atr[1];
//c_value=c_value.replace("'","");
c.value=c_value;
}
else
{
atr=arr[l].split(" ");
c.id=l+1;
c.entity=atr[0];
c.operator=atr[1];
String c_value=atr[2];
//c_value=c_value.replace("'","");
c.value=c_value;
}
/*for(int k=0;k<arr2.size();k++)
{
if(arr[l].contains(" BETWEEN "))
{
System.out.println("inside if");
atr=arr[l].split(" BETWEEN ");
c.id=l+1;
c.entity=atr[0];
c.operator=" BETWEEN ";
String c_value=atr[1];
c_value=c_value.replace("'","");
c.value=c_value;
System.out.println(c.entity+" "+c.operator+" "+c.value );
}
else
{
System.out.println("inside else");
atr=arr[l].split(" ");
for(int o=0;o<atr.length;o++)
{
arr2.push(atr[o].toString());
}
c.id=l+1;
c.entity=atr[0];
c.operator=atr[1];
String c_value=atr[2];
c_value=c_value.replace("'","");
c.value=c_value;
}
}*/
c.enopva=arr[l];
int c_id=getDecompressedString(arr,orig_input,l);
if (c_id==0)
{
c.parent_id=c_id;
}
else if(c_id>0)
{
c.parent_id=c_id;
}
if(l>=bool_op.length)
{
c.bool_op=null;
}
else if(l<bool_op.length)
{
c.bool_op=bool_op[l].toString();
}
IncentiveProLog.insertLog(" Class has been generated as "+c.toString(),id);
try
{
insertData(c.id,c_id,c.entity,c.operator,c.value,c.bool_op);
}
catch (SQLException e)
{
e.printStackTrace();
}
Related
I am trying to capture allowed and disallowed rules of robots.txt file in java using following code:-
package robotest;
public class RoboTest {
public static void main(String[] args) {
String robo="user-agent:hello user-agent:ppx user-agent:bot allow:/world disallow:/ajax disallow:/posts user-agent:abc allow:/myposts/like disallow:/none user-agent:* allow:/world";
String[] strarr=robo.split(" ");
String[] allowed={};
String[] disallowed={};
boolean new_block=false;
boolean a_or_d=false;
for (String line: strarr){
if(line!=""){
if(line.contains("user-agent:pp")==false && a_or_d){
break;
}
if (line.contains("user-agent:ppx")||(new_block )){
new_block=true;
System.out.println(line);
if(line.contains("allow") || line.contains("disallow")){
a_or_d=true;
}
if(line.contains("allow:")){
//append to allowed
}
if(line.contains("disallowed")) {
//append to disallowed
}
}
}
System.out.println(allowed);;
}
}
}
The code does not works properly as I expect. The rules of robots.txt string is separated by white space. I want to capture rules of user-agent ppx. The code should look for allow or disallow block after discovering user-agent:ppx and append them to list. But it is not working and is confusing too. I am also new to regex in java. What can be solution for this.
Some minimum modifications to your code:
String robo = "user-agent:hello user-agent:ppx user-agent:bot allow:/world disallow:/ajax disallow:/posts user-agent:abc allow:/myposts/like disallow:/none user-agent:* allow:/world";
String[] strarr = robo.split(" ");
Set<String> allowed = new HashSet<>();
Set<String> disallowed = new HashSet<>();
Pattern allowPattern = Pattern.compile("^allow:\\s*(.*)");
Pattern disallowPattern = Pattern.compile("^disallow:\\s*(.*)");
boolean isUserAgentPpx = false;
boolean a_or_d = false;
for (String line : strarr) {
line = line.trim();
// Skip empty lines
if (line.isEmpty()) continue;
if (line.startsWith("user-agent:")) {
// If previous lines were allowed/disallowed rules, then start a new user-agent block
if (a_or_d) {
a_or_d = false;
isUserAgentPpx = false;
}
// Skip block of user-agent if we already found 'user-agent: ppx' or 'user-agent: *'
if (isUserAgentPpx) continue;
if (line.matches("^user-agent:\\s*(ppx|\\*)$")) {
isUserAgentPpx = true;
}
continue;
}
// Process block of allow/disallow
a_or_d = true;
if (isUserAgentPpx) {
Matcher allowMatcher = allowPattern.matcher(line);
if (allowMatcher.find()) {
allowed.add(allowMatcher.group(1));
}
Matcher disallowMatcher = disallowPattern.matcher(line);
if (disallowMatcher.find()) {
disallowed.add(disallowMatcher.group(1));
}
}
}
System.out.println("Allowed rules for Ppx:");
for (String s : allowed) {
System.out.println(s);
}
System.out.println("Disallowed rules for Ppx:");
for (String s : disallowed) {
System.out.println(s);
}
I'm using Set<String> to store the rules to avoid duplicates.
I made it little easy. Beware of edge conditions though
public class RoboTest {
public void test() {
String robo = "user-agent:hello user-agent:ppx allow:/aellow disallow:/deasllow disallow:/posts user-agent:bot allow:/world disallow:/ajax disallow:/posts user-agent:abc allow:/myposts/like disallow:/none user-agent:* allow:/world";
String[] strarr = robo.split(" ");
List<String> allowed = new ArrayList<>();
List<String> disAllowed = new ArrayList<>();
boolean checkAllowed = false;
for (String line : strarr) {
if (line.contains("user-agent:ppx")) {
checkAllowed = true;
continue;
} else if (checkAllowed) {
if (line.contains("disallow:")) {
disAllowed.add(line.split(":")[1]);
continue;
}
if (line.contains("allow:")) {
allowed.add(line.split(":")[1]);
continue;
}
checkAllowed = false;
}
}
System.out.println("Allowed" + allowed);
System.out.println("DisAllowed" + disAllowed);
}
}
I want to make a configuration to store items, however, when I was making the paths to get the values, something wrong happened.
HashMap<String, Text> sections;
private void loadKeys() {
List<String> list = new ArrayList<>();
for (String s : sections.keySet()) {
Text te = sections.get(s);
String changeable = s.substring(0, s.length() - 1);
for (int i = 0; i < te.lines(); i++) {
String line = te.getLine(i);
while (line.startsWith(" ")) {
line = line.substring(2);
}
if (!line.startsWith("-")) {
if (line.endsWith(":")) {
changeable = changeable + "." + line.substring(0, line.length() - 1);
} else {
list.add(changeable + "." + line);
}
}
}
}
for (String s : list) {
System.out.println(s);
}
}
Text.java
public class Text {
private List<String> lines = new ArrayList<>();
public Text(String txt) {
if (txt.contains("\n")) {
for (String s : txt.split("\n")) {
lines.add(s);
}
} else {
lines.add(txt);
}
}
public int lines() {
return lines.size();
}
public String getLine(int line) {
return lines.get(line);
}
#Override
public String toString() {
String string = "";
for (String s : lines) {
if (string.equals("")) {
string = s;
} else {
string = string + "\n" + s;
}
}
return string;
}
}
File:
Test11:
Test12:
Test13: 'test'
Test14: 'test2'
Test15: teste
Test16:
Test17: "test test"
The output I want:
Test11.Test12.Test13: 'test'
Test11.Test12.Test14: 'test2'
Test11.Test15: teste
Test11.Test16.Test17: "test test"
What I got with the code above:
Test11.Test12.Test13: 'test'
Test11.Test12.Test14: 'test2'
Test11.Test12.Test15: teste
Test11.Test12.Test16.Test17: "test test"
Test12 is being repeated. Can you help me have what I want? Thanks in advance
It is pretty easy. All you need is just keep current level depth and level name. You can do it via recursion or using queue.
public static Map<String, String> readProperties(Path path) throws IOException {
final class Level {
private final String name;
private final int offs;
public Level(String name, int offs) {
this.name = name;
this.offs = offs;
}
}
Map<String, String> map = new LinkedHashMap<>();
// contains all root items for current one with it's offset, to detecl that current level is sub level or parent
Deque<Level> levels = new LinkedList<>();
Pattern pattern = Pattern.compile("(?<offs>\\s*)(?<key>[^:]+)\\s*:\\s*(?<value>.*)\\s*");
Files.lines(path)
.map(pattern::matcher)
.filter(Matcher::matches)
.forEach(matcher -> {
int offs = matcher.group("offs").length();
// remove parent levels until reach the parent of current level
while (!levels.isEmpty() && levels.peekLast().offs >= offs) {
levels.removeLast();
}
String key = matcher.group("key");
String value = matcher.group("value");
if (value.isEmpty())
levels.add(new Level(key, offs));
else
map.put(levels.stream().map(level -> level.name).collect(Collectors.joining(".")) + '.' + key, value);
});
return map;
}
I write this code who iterate an arraylist and if found a name who's equal to getName it's will print that and if not found will print "a message". My problem with this code is:
if a person have same nome of another persone the program will stop at first match because of break; how can i fix that?
import java.util.*;
public class AggPersone {
public static void main(String[] args) {
ArrayList<Item> voce = new ArrayList<Item>();
voce.add(new Item("Robert", "Via qualcosa", "123"));
voce.add(new Item("Roberto","Via qualcosina", "123"));
voce.add(new Item("Robert", "Via qual ", "2222"));
Scanner input = new Scanner(System.in);
System.out.println("chi cerchi?");
String chiave = input.nextLine();
int i = 0;
while(i < voce.size()){
if(voce.get(i).getNome().equals(chiave)){
System.out.println(voce.get(i).toString());
break;
}
i++;
if(i == voce.size()){
System.out.println("Nessun match");
}
}
input.close();
}
}
Use a boolean variable to flag if the name has been found:
boolean found = false;
for (Item item : voce) {
if(item.getNome().equals(chiave)){
System.out.println(item.toString());
found = true;
}
}
if (!found) {
System.out.println("Nessun match");
}
I have a java binary search tree and I want to create a menu.
To this day I used StreamTokenizer to get the user input,
But now it doesn't seem to work with "+", "-", "?".
My code:
public void listen() throws IOException {
boolean stay = true;
System.out.println("Give me commands .. ");
while(stay) {
tokens.nextToken();
if(tokens.sval.equals("+")) {
tree.insert(new PositiveInt((int) tokens.nval));
} else if(tokens.sval.equals("?")) {
System.out.println(
tree.retrieve(new PositiveInt((int) tokens.nval)) == null ? "Not exist" : "exist");
} else if(tokens.sval.equals("-")) {
tree.remove(new PositiveInt((int) tokens.nval));
} else if(tokens.sval.equalsIgnoreCase("K")) {
tree.writeKeys();
} else if(tokens.sval.equalsIgnoreCase("E")) {
System.out.println("Empty = " + tree.isEmpty());
} else if(tokens.sval.equalsIgnoreCase("F")) {
System.out.println("Full = " + tree.isFull());
} else if(tokens.sval.equalsIgnoreCase("C")) {
tree.clear();
} else if(tokens.sval.equalsIgnoreCase("P")) {
tree.showStructure();
} else if(tokens.sval.equalsIgnoreCase("Q")) {
stay = false;
} else {
System.out.println("Unaccaptable input.");
}
}
}
When I enter "P" , for example, or any other character, everything's alright.
When I enter "?", "+", "-", I'm getting:
Exception in thread "main" java.lang.NullPointerException
at TestBSTree.listen(TestBSTree.java:27)
at TestBSTree.main(TestBSTree.java:54)
As Line 27 is :
if(tokens.sval.equals("+")) {
In other words, a non-charater is not accaptable with the tokenizer.
Why and how can I fix it?
Whole code:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class TestBSTree {
// Test class variables
BSTree<PositiveInt> tree;
InputStreamReader reader;
StreamTokenizer tokens;
PositiveInt key;
int in;
public TestBSTree(PositiveInt root) {
tree = new BSTree<PositiveInt>(new BSTreeNode<>(root, null, null));
reader = new InputStreamReader(System.in);
tokens = new StreamTokenizer(reader);
key = null;
}
public void listen() throws IOException {
boolean stay = true;
System.out.println("Give me commands .. ");
while(stay) {
tokens.nextToken();
if(tokens.sval.equals("+")) {
tree.insert(new PositiveInt((int) tokens.nval));
} else if(tokens.sval.equals("?")) {
System.out.println(
tree.retrieve(new PositiveInt((int) tokens.nval)) == null ? "Not exist" : "exist");
} else if(tokens.sval.equals("-")) {
tree.remove(new PositiveInt((int) tokens.nval));
} else if(tokens.sval.equalsIgnoreCase("K")) {
tree.writeKeys();
} else if(tokens.sval.equalsIgnoreCase("E")) {
System.out.println("Empty = " + tree.isEmpty());
} else if(tokens.sval.equalsIgnoreCase("F")) {
System.out.println("Full = " + tree.isFull());
} else if(tokens.sval.equalsIgnoreCase("C")) {
tree.clear();
} else if(tokens.sval.equalsIgnoreCase("P")) {
tree.showStructure();
} else if(tokens.sval.equalsIgnoreCase("Q")) {
stay = false;
} else {
System.out.println("Unaccaptable input.");
}
}
}
public static void main(String[] args) throws IOException {
TestBSTree test = new TestBSTree(new PositiveInt(0));
test.listen();
}
}
It doesn't matter how does the tree or PositiveInt implemented, the main issue is the tokenizer.
if you want so split a string containg a '?' or a plus ('+'), you cannot simply use this symbol to split this String; they are reserved 'words' and need a exclude sign '\' which itself needs an exclusion sign ^^ (so you need two '\\' and the special sign)
try to use something like that:
StringTokenizer tokenizer = new StringTokenizer("ghj?klm", "\\?");
System.out.println(tokenizer.countTokens() );
-> result: the count is 2 !
you can also apply this method for String.split("\+");
//wont work!!
String str = "ghj?klm";
String[] s = str.split("?");
System.out.println(s.length );
but this code will work!
String str = "ghj?klm";
String[] s = str.split("\\?");
System.out.println(s.length );
it's the same 'problem' ^^ i hope this helped!
unfortunaltely i don't know which other symbols require a slahs... :-(
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
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
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
The above code is currently printing this summary data
11/2/2010 cat 20 3
11/3/2010 cat 104 4
11/4/2010 cat 26 2
I need help is printing the summary as shown below
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
i hav bulk of data seperated from "," . so iwant to read line and split. & i hav done it. but my requrment is above shown result.
Below is the code to do it. I am taking help of google-guava libraries as it helps me write less code ;-). If you just want in plain java then you can modify the code also if the logic needs some tweaking then look at processLine(...) method, that is where the change will go
Ok the only missing code I see is printing empty data for the dates that are not part of the input file in a sorted order. That is simple and leave it to you. Here is the hint: Increment date by 1 & loop until end of the month
I have run your sample file and it prints the below summary
11/3/2010 cat 89 3
11/3/2010 dog 3 1
11/2/2010 dog 4 1
11/2/2010 cat 20 3
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
public class AnimalSummaryBuilder
{
private static final Splitter SPLITTER = Splitter.on(CharMatcher.anyOf(","));
private static final Joiner JOINER = Joiner.on("\t");
#SuppressWarnings("unchecked")
public static void main(final String[] args) throws Exception
{
#SuppressWarnings("rawtypes")
Map<Animal, Summary> result = Files.readLines(new File("c:/1.txt"), Charsets.ISO_8859_1, new LineProcessor() {
private final Map<Animal, Summary> result = Maps.newHashMap();
public Object getResult()
{
return result;
}
public boolean processLine(final String line) throws IOException
{
Iterator<String> columns = SPLITTER.split(line).iterator();
String date = columns.next();
String name = columns.next();
int value = Integer.valueOf(columns.next()).intValue();
Animal currentRow = new Animal(date, name);
if (result.containsKey(currentRow))
{
Summary summary = result.get(currentRow);
summary.increaseCount();
summary.addToTotal(value);
}
else
{
Summary initialSummary = new Summary();
initialSummary.setCount(1);
initialSummary.setTotal(value);
result.put(currentRow, initialSummary);
}
return true;
}
});
for (Map.Entry<Animal, Summary> entry : result.entrySet())
{
Animal animal = entry.getKey();
Summary summary = entry.getValue();
System.out.println(JOINER.join(animal.date, animal.name, summary.total, summary.count));
}
}
final static class Animal
{
String date;
String name;
public Animal(final String date, final String n)
{
this.date = date;
this.name = n;
}
#Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (!(obj instanceof Animal))
{
return false;
}
Animal other = (Animal) obj;
if (date == null)
{
if (other.date != null)
{
return false;
}
}
else if (!date.equals(other.date))
{
return false;
}
if (name == null)
{
if (other.name != null)
{
return false;
}
}
else if (!name.equals(other.name))
{
return false;
}
return true;
}
}
final static class Summary
{
private int total;
private int count;
void setTotal(int value)
{
total = value;
}
void setCount(int i)
{
count = i;
}
void increaseCount()
{
count++;
}
void addToTotal(int valueToAdd)
{
total += valueToAdd;
}
}
}
You could use another map with the date as the key, and the results you got as value. Then you just loop through all the days in the month, and if the map contains the current date key, you print the corresponding value, else you only print the date.
Here is the dirty solution. The assumption is that the "result" map contains only 1 month.
public class Reader
{
public static void main(final String[] args) throws ParseException
{
BufferedReader reader = null;
String animalName = "cat";
// String animal = null;
try
{
reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/1.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);
}
}
String date = result.keySet().iterator().next();
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Calendar calendar = Calendar.getInstance();
calendar.setTime(df.parse(date));
int monthStart = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
int monthEnd = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 0);
// interate and print new output
for (int i = monthStart; i < monthEnd; i++)
{
calendar.add(Calendar.DAY_OF_MONTH, 1);
String key = df.format(calendar.getTime());
if (result.containsKey(key))
{
Integer sum = result.get(key);
Integer count = result2.get(key);
System.out.println(key + " " + animalName + " " + sum + "\t" + count);
}
System.out.println(key);
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
reader.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}