Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

how to print the duplicate letters in a string?

on Thursday, July 17, 2014
Below Code may help you for this.

package general;

public class PrintDuplicateLetterInString {

public static void main(String[] args) {

//String ip="navneet";
String ip="aaaa";
char [] input = ip.toCharArray();
System.out.println("Duplicate Characters are as follows:");
for(int i=0;i<ip.length();i++)
{
for(int j=i+1;j<ip.length();j++)
{
if(input[i]==input[j])
{
System.out.println(input[j]);
break;
}
}
}


}

}

How to get operating system information in Java

on Wednesday, July 16, 2014
Below code may help you to get Operating System information:

               // it gives operating system info
String osname = System.getProperty("os.name");
System.out.println(osname);

// it gives all the information regarding system
Properties prop = System.getProperties();
                  Set<Object> keySet = prop.keySet();
                  for(Object obj : keySet){
                     system.out.println("System Property:                                                                                                            {"+obj.toString()+","+System.getProperty(obj.toString())+"}");

Can we compile a java file with a different name than the class

yes, we compile a java file with a different name than the class, provided that there should not be any public class in that file.

If there is any public class in file then in that case you have to give that name as file name. But if your class does not contain any public class then you can give any name to you class.

Please refer below example to make it more clear:

file name : sample.java

class A
{
   public static void main(String args[])
   {
      System.out.println("hi in Class A");
   }
}
class B
{
   public static void main(String args[])
   {
     System.out.println("hello in class B");
   }
}


then compile it with(windows) : javac sample.java

then run it : java A

output :  hi in Class A

then run it : java B

output :  hello in class B


Please check and confirm.

How to Get Current Date/ System Date(sysdate) in Java

In Java, you can get the current date time via following two classes: 
1. Date
2.Calendar
Moreover,  you can use SimpleDateFormat class to convert the date into user friendly format.

Please refer below code to understand it properly.

                1. Date with SimpleDateFormat Class

                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
String currentDate = dateFormat.format(date);
System.out.println(currentDate);

               2. Calendar with SimpleDateFormat Class

                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance();
String currentDate=dateFormat.format(cal.getTime());
System.out.println(currentDate);

Also you can use the following code:

                 Calendar cal = null;
              String currentDate = "";
               cal = Calendar.getInstance();
               currentDate = javaCalendar.get(Calendar.DATE) +
                                      "/" + (javaCalendar.get(Calendar.MONTH) + 1) +
                                     "/" + javaCalendar.get(Calendar.YEAR);
 System.out.println(currentDate);

To Prevent From Throwing Null Pointer Exception

on Tuesday, July 15, 2014
Always write string before the string variable while using equals function.
example:
if("".equals(variable))
rather than
if(variable.equals(""))

Here if the string variable is null then it will throw Null Pointer Exception.
So for best practice always write string before the string variable while using equals function.