Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMaps

 
sirisha makkapati
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi To All,


Actully i want one logic,please any one telle.
I have HashMap which will strart jan-2007=4443.778,feb-2007=56556.77 like this up to dec-2009(Here key is month && Year)this is Parameter to my method.
I have to return previous year value for current Year.this is my output.for jan-2007 there would be no Previous year thts why I have to return Zero.

for example out put is Like this:

Jan-2007=0,feb-2007=0,...................jan-2008=4443.778,feb-2008=56556.77 ...............

please any one help me.

I had written a code but it is not returning Exactly.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us the code you've written (in code tags) and tell us what it is doing that you're not expecting.
 
sirisha makkapati
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

package mypackage;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

public class dateDisplay
{

public static void main(String args[])
{
LinkedHashMap condLinkMap=new LinkedHashMap();
dateDisplay obj=new dateDisplay();
condLinkMap.put("JAN-2007", "78.98");
condLinkMap.put("FEB-2007", "54.80");
condLinkMap.put("MAR-2007", "69.22");
condLinkMap.put("APR-2007", "33.78");
condLinkMap.put("MAY-2007", "98.99");
condLinkMap.put("JUN-2007", "45.99");
condLinkMap.put("JUL-2007", "66.77");
condLinkMap.put("AUG-2007", "12.45");
condLinkMap.put("SEP-2007", "32.23");
condLinkMap.put("OCT-2007", "78.65");
condLinkMap.put("NOV-2007", "34.91");
condLinkMap.put("DEC-2007", "98.54");

condLinkMap.put("JAN-2008", "99.45");
condLinkMap.put("FEB-2008", "54.66");
condLinkMap.put("MAR-2008", "78.54");
condLinkMap.put("APR-2008", "22.55");
condLinkMap.put("MAY-2008", "15.95");
condLinkMap.put("JUN-2008", "65.15");
condLinkMap.put("JUL-2008", "78.32");
condLinkMap.put("AUG-2008", "35.75");
condLinkMap.put("SEP-2008", "81.15");
condLinkMap.put("OCT-2008", "34.15");
condLinkMap.put("NOV-2008", "45.77");
condLinkMap.put("DEC-2008", "88.66");

condLinkMap.put("JAN-2009", "10.22");
condLinkMap.put("FEB-2009", "20.43");
condLinkMap.put("MAR-2009", "30.45");
condLinkMap.put("APR-2009", "40.65");
condLinkMap.put("MAY-2009", "50.23");
condLinkMap.put("JUN-2009", "60.99");
condLinkMap.put("JUL-2009", "70.09");
condLinkMap.put("AUG-2009", "80.333");
condLinkMap.put("SEP-2009", "90.11");
condLinkMap.put("OCT-2009", "100.109");
condLinkMap.put("NOV-2009", "90.33");
condLinkMap.put("DEC-2009", "190.66");
System.out.println(obj.display(condLinkMap));

}
public LinkedHashMap display(LinkedHashMap condMap)
{
double year=0.0;
String preYear=null;
String value=null;
String y=null;
String year1=null;
LinkedHashMap resultMap=new LinkedHashMap();
double var=0.0;
double d=0.0;
double newYear=0.0;
Set s=condMap.keySet();
Iterator i=s.iterator();
try
{
while(i.hasNext())
{
preYear=(String)i.next();
year= Double.parseDouble(preYear.substring(4,preYear.length()));
if(year==2007)
{
value=(String)condMap.get(preYear);
resultMap.put(preYear,Double.toString(0));
System.out.println(value);
}

else
{
value=(String)condMap.get(preYear);
year=Double.parseDouble(preYear.substring(4,preYear.length()));
newYear=(year) + 1;
year1=preYear.replaceAll(Double.toString(year),Double.toString(newYear));
value=(String)condMap.get(preYear);
resultMap.put(preYear,value);
}
}
}

catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
return resultMap;
}
}

This is my code,result is coming like below,its not giving previous year values.
{JAN-2007=0.0, FEB-2007=0.0, MAR-2007=0.0, APR-2007=0.0, MAY-2007=0.0, JUN-2007=0.0, JUL-2007=0.0, AUG-2007=0.0, SEP-2007=0.0, OCT-2007=0.0, NOV-2007=0.0, DEC-2007=0.0, JAN-2008=99.45, FEB-2008=54.66, MAR-2008=78.54, APR-2008=22.55, MAY-2008=15.95, JUN-2008=65.15, JUL-2008=78.32, AUG-2008=35.75, SEP-2008=81.15, OCT-2008=34.15, NOV-2008=45.77, DEC-2008=88.66, JAN-2009=10.22, FEB-2009=20.43, MAR-2009=30.45, APR-2009=40.65, MAY-2009=50.23, JUN-2009=60.99, JUL-2009=70.09, AUG-2009=80.333, SEP-2009=90.11, OCT-2009=100.109, NOV-2009=90.33, DEC-2009=190.66}

please tell me the solution
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think these two lines are your problem

You are using the same key to both get the value out of the condMap and put it in the resultMap. You need to use the current year to put the value into the resultMap.
 
sandhya shetty
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Actually problem in your code is here:

newYear=(year) + 1;
year1=preYear.replaceAll(Double.toString(year),Double.toString(newYear));
value=(String)condMap.get(preYear);
resultMap.put(preYear,value);

1. you are trying to put in the resultmap ,the key & value belonging to same year.(key-preYear,value-corresponding to pre value)
2.Also replaceAll is not working here ,try to ouput the value of year1 & you will know its not getting replaced.And so further processing fails.
3.newyear for which you are trying to replace should be year-1 and not year +1.


solution:
Declare datatype for year as long & not double.because then you dont have to worry for truncating the double when used in replaceAll method.


Well,here is the solution, you can just copy paste the method:


public LinkedHashMap display(LinkedHashMap condMap)
{
String currYearAndMonth=null;
String preYearAndMonth=null;
long currYear=0;
String value=null;
LinkedHashMap resultMap=new LinkedHashMap();
Set s=condMap.keySet();
Iterator i=s.iterator();
try
{
while(i.hasNext())
{
currYearAndMonth=(String)i.next();
currYear= Long.parseLong(currYearAndMonth.substring(4,currYearAndMonth.length()));
if(currYear==2007)
{
resultMap.put(currYearAndMonth,Long.toString(0));
}
else
{
long preYear=currYear - 1;
preYearAndMonth=currYearAndMonth.replaceAll(Long.toString(currYear),Long.toString(preYear));
value=(String)condMap.get(preYearAndMonth);
resultMap.put(currYearAndMonth,value);
}
}
}

catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
return resultMap;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic