Monday, July 13, 2009

Get your horo scope

It's a small program which can be used for display horoscope in u'r web site or application try this

package sms;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Astrology {

/**
* This method will return your horoscope according to the date passed as argument
* @param date DD-MM-YYYY the date of birth
* @return : string
* @throws Exception Invalid Date format
*/
public static String getYourHoroscope(String date) throws Exception{
Pattern datePattern = Pattern.compile("(\\d{2})-(\\d{2})-(\\d{4})");
Matcher dateMatcher = datePattern.matcher(date);
if(!dateMatcher.matches())
throw new Exception("Invalid date format");
String yourHoroscope="";
Hashtable horoscope=new Hashtable();
horoscope=getHoroscope();
int day=0,month=0;
day=Integer.parseInt(dateMatcher.group(1));
month=Integer.parseInt(dateMatcher.group(2));
System.out.println("month:"+month);
System.out.println("day:"+day);
if(((month==12)&&(day>=21))||((month==1)&&(day<=19))){
yourHoroscope=horoscope.get("capricon");
} else if(((month==1)&&(day>=20))||((month==2)&&(day<=18))){
yourHoroscope=horoscope.get("aquarius");
} else if(((month==2)&&(day>=19))||((month==3)&&(day<=20))){
yourHoroscope=horoscope.get("pisces");
} else if(((month==3)&&(day>=21))||((month==4)&&(day<=19))){
yourHoroscope=horoscope.get("arius");
} else if(((month==4)&&(day>=20))||((month==5)&&(day<=20))){
yourHoroscope=horoscope.get("taurus");
} else if(((month==5)&&(day>=21))||((month==6)&&(day<=21))){
yourHoroscope=horoscope.get("gemini");
} else if(((month==6)&&(day>=22))||((month==7)&&(day<=22))){
yourHoroscope=horoscope.get("cancer");
} else if(((month==7)&&(day>=23))||((month==8)&&(day<=22))){
yourHoroscope=horoscope.get("leo");
} else if(((month==8)&&(day>=23))||((month==9)&&(day<=22))){
yourHoroscope=horoscope.get("virgo");
} else if(((month==9)&&(day>=23))||((month==10)&&(day<=22))){
yourHoroscope=horoscope.get("libra");
} else if(((month==10)&&(day>=23))||((month==11)&&(day<=21))){
yourHoroscope=horoscope.get("scorpio");
} else if(((month==11)&&(day>=22))||((month==12)&&(day<=21))){
yourHoroscope=horoscope.get("sagittarius");
}
return yourHoroscope;
}

/**
* This method used to collect horoscope from the web site
* @return return a hash table
*/
public static Hashtable getHoroscope(){
URL url=null;
Hashtable horoscope=new Hashtable();
try {
url = new URL("http","192.168.0.6",3128,"http://www.sxmsms.com/horoscope.aspx");
HttpURLConnection uc=null;
uc = (HttpURLConnection) url.openConnection();
BufferedReader br=null;
br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String temp="";
StringBuffer data=new StringBuffer();
while ( (temp = br.readLine()) != null ) {
data.append(temp);
}
String content=data.toString();
int startIndex=0;
int endIndex=0;
if(content.indexOf("
startIndex=content.indexOf("
content=content.substring(startIndex);
endIndex=content.indexOf("
");
content=content.substring(0,endIndex);
content=content.substring(content.indexOf("")+8,content.lastIndexOf("")+4);
}
String []strs=content.split("");
String key="";
String msg="";
for(int i=0;i key=strs[i].substring(0,strs[i].indexOf("
")).toLowerCase();
msg=strs[i].substring(strs[i].indexOf("SMS\">")+5,strs[i].lastIndexOf(""));
horoscope.put(key, msg);
}
} catch (Exception e) {
e.printStackTrace();
}
return horoscope;
}


public static void main(String[] args)throws Exception {
System.out.println(getYourHoroscope("10-09-1983"));
}
}

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails