Tuesday, 20 November 2012

Importance of toString() in java


Object class of Java is having predefined toString() method. This method by default Object class calls implicitly when an object created. Overriding toString manually is nothing but implementing this method in our class.The java toString() method is used when we need a string representation of an object. It is defined in Object class. 

The toString() method is useful for debugging. By default, when an object is printed out in a print stream like System.out, the toString() method of the object is automatically called.
While develping the code the developers used to check the object properties are getting through the object or not. For this print statement will be useful to quick test in the console.
public String toString()
Returns: a string representation of the object.

Let us take a Person class and try to print the person object using the System.out.println() statement. 

package blog.javabynataraj;
//@author Muralidhar N
class Person{
 public String fname;
 public String lname;
  
 Person(String fn,String ln){
  this.fname=fn;
  this.lname=ln;
 }
 public String getFname() {
  return fname;
 }
 public void setFname(String fname) {
  this.fname = fname;
 }
 public String getLname() {
  return lname;
 }
 public void setLname(String lname) {
  this.lname = lname;
 }
}
public class ToStringTest {
 public static void main(String[] args) {
  Person p = new Person("murali","dhar");
  System.out.println(p);
 }
}


The output will be the class name@hexadecimal



here the Object class's method toString returning
getClass().getName()+'@'+Integer.toHexString(hashCode())
But here we assumed that the firstname and the lastname will be print. But it is not done. This is the magic toStirng method.

But in the below program we can print the firstname and lastname of person Object. What we are going to do here is just overriding the toString method in Person Class.


package blog.javabynataraj;
//@author Muralidhar N
class Person{
 public String fname;
 public String lname;
  
 Person(String fn,String ln){
  this.fname=fn;
  this.lname=ln;
 }
 public String getFname() {
  return fname;
 }
 public void setFname(String fname) {
  this.fname = fname;
 }
 public String getLname() {
  return lname;
 }
 public void setLname(String lname) {
  this.lname = lname;
 }
 public String toString(){
  return (getClass()+"  FirstName: "+fname+"  LastName: "+lname);
 }
}
public class ToStringTest {
 public static void main(String[] args) {
  Person p = new Person("murali","dhar");
  System.out.println(p);
 }
}

See the output of this: 
Now you can achieve this by overriding the default toString() method inside Person class to return the contents of the instance of Person.

http://javabynataraj.blogspot.in/2012/10/importance-of-tostring-in-java.html


Thanks for visiting.........


Thursday, 8 November 2012

How to read XML response from a URL in java and return as String

How to read XML response from a URL in java and return as String


Sol:

public String getXXX(String feedId){
String sql = null;
String UrlString =null;
String output = "";
try{
sql = AppUtil.getProperty("Q_GET_FEED");

Formatter fmt = new Formatter();
sql = fmt.format(sql, feedId).toString();

List l = (List)AppUtil.runAQueryCurrentSession(sql,Feeder.class);
Feeder feeder = (Feeder) l.get(0);
UrlString = feeder.getLink();

// open url connection
URL url = new URL(UrlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();

// set up url connection to get retrieve information back
con.setRequestMethod("GET");
con.setDoInput(true);

// pull the information back from the URL
InputStream is = con.getInputStream();
StringBuffer buf = new StringBuffer();
int c;
while ((c = is.read()) != -1) {
buf.append((char) c);
}
con.disconnect();

output = buf.toString();

// Just throw the output from the URL
return output;

}catch(Exception e){
e.printStackTrace();
log.error(" Error in retriveing feed data"
+ e.getMessage());
return "";
}

}

Tuesday, 23 October 2012

Maven : Error Build fails "org.mortbay.jetty:maven-jetty-plugin"

The build fails:
Unable to find resource 'org.mortbay.jetty:maven-jetty-plugin:maven-plugin:7.0.0.pre5'. 



Solution: 


Index: example/webapp/pom.xml
===================================================================
--- example/webapp/pom.xml (revision 807)
+++ example/webapp/pom.xml Mon Nov 24 14:29:19 EST 2008
@@ -32,7 +32,7 @@
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
- <artifactId>maven-jetty-plugin</artifactId>
+ <artifactId>jetty-maven-plugin</artifactId>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppSourceDirectory>${basedir}/src/main/webapp/</webAppSourceDirectory>
Index: example/oauth-provider/pom.xml
===================================================================
--- example/oauth-provider/pom.xml (revision 807)
+++ example/oauth-provider/pom.xml Mon Nov 24 14:29:35 EST 2008
@@ -36,7 +36,7 @@
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
- <artifactId>maven-jetty-plugin</artifactId>
+ <artifactId>jetty-maven-plugin</artifactId>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppSourceDirectory>${basedir}/web</webAppSourceDirectory>

Error: Java compiler level does not match the version of the installed Java project facet

Error:

Java compiler level does not match the version of the installed Java project facet


Eclipse or STS:  change the value shown in below screen...




Thanks for Visiting.........................

Monday, 1 October 2012

Installing Subclipse in Flash Builder 4


Subclipse is one of the most frequently used plug-ins with Flash Builder. Here are some simple instructions for installing Subclipse and importing an existing project.
  1. Help > Install New Software…
  2. Add…
  3. Name=Subclipse, Location=http://subclipse.tigris.org/update_1.6.x
  4. Select the plug-ins shown in the screenshot below. Note: I’ve deselected a few plug-ins here in order to minimize plug-in dependencies that sometimes have issues resolving.
    subclipse1.png
  5. Finish the wizard. When prompted, restart Eclipse.
  6. On Mac, since JavaHL is not available, be sure to select SVNKit for your SVN interface
    1. Window > Preferences
    2. Team > SVN
    3. Change SVN Interface to SVNKit
  7. To import your projects from SVN
    1. File > Import…
    2. SVN > Checkout Projects from SVN
    3. Create or select a repository location
    4. Select your project root folder
    5. Finish

Thursday, 13 September 2012

Handling Date and Time in Flex

Handling Date and Time in Flex


Pattern Example
Year      
YY              09 (Year Number Short)
YYY           2009 (Year Number Full)
YYYY        02009 (I have no clue why you would need this)
Month 
M                7 (Month Number Short)
MM            07 (Month Number Full)
MMM         Jul (Month Name Short)
MMMM     July (Month Name Full)
Day       
D               4 (Day Number Short)
DD            04 (Day Number Full)
Day Name          
E               1 (Day Number Short)
EE            01 (Day Number Full)
EEE          Mon (Day Name Short)
EEEE        Monday (Day Name Full)
A              AM/PM
J               Hour in day (0-23)
H              Hour in day (1-24)
K              Hour in AM/PM (0-11)
L               Hour in AM/PM(1-12)
N             3 (Minutes)
NN          03 (Minutes)
SS           30 (Seconds)


Sample date format : 
MXML:
<fx:Declarations>
<mx:DateFormatter id="dateFormatter" formatString="MM/DD/YYYY"/>
<mx:DateFormatter id="dateFormatterForDateAndTime" formatString="MM/DD/YYYY L:NN A"/>
</fx:Declarations>


Date time Component:




Thanks for Visiting............

Wednesday, 12 September 2012

Icon in addressbar (Favicon in flex)


Favicon(Icon in addressbar) in flex
Hi friends,
After too much googling finally I found the way how to display the favicon i.e icon display in the website in the left hand side of address bar.
You have to just put the two lines in the html page.

---------------------------------------------------------------------
"< link rel="icon" href="favicon.ico" type="image/ico">"
---------------------------------------------------------------------

place this line inside the head tag of your applicationName.html

You can find the html file in
YourApplicationFolder/bin-debug/yourApplicationName.html


HREF= address of your icon

Just compile and run your project.
You will find the icon in address bar.


FYI - To convert .jpg, gif ..etc to .ico  use below web site.
http://www.coolutils.com/online/image-converter/

Thanks for visiting...........................