Wednesday, June 1, 2011

How to create user defined javascript object

As all the computer languages are moving towards the Object Oriented approach. Javascript is also having one of the most powerful feature of creating objects in javascript. I came across many developers who say javascript is just about the validation, but in my view , this is one the main component of developing web based application. I am not sure why this language is termed as validation language as well by many, but when you go deep into this language , this is one of the best language to handle to browser based application and formatting the data before getting displayed on the User Interface. Keeping this in mind, i am sharing one of best feature of javascript of creating user defined objects. This feature can be used at major places such as displaying data in the tabular format, creating a object of your own choice to parse , display or manipulate in any manner you like.......here is the sample code for the same.....





Click on the image if unable to see it clearly.

Tuesday, October 6, 2009

What is HTML ?

The full name of HTML is Hyper Text Markup Language, majorly used in static developing Web Pages. Static web pages means pages where the content of the page is fixed means any article, definition etc, In other words, non-interactive data. This HTML code is merged with some other client side scripting such as php,  javascript, vb script etc and generated dynamic content to web pages.

Sample HTMLPage

<HTML>

<BODY>

Hello, How are you Subu!!!

</BODY>

</HTML>

del.icio.us Tags:

Sunday, March 8, 2009

Java example to list all the items from the Folder/Location

As i always believe, Java is very powerfull language, providing us many different ways to use a specific task, here is one of the ways to list all the files or folders within the Folder location through the java program.
FolderFileList.java
--------------------
/* File : FolderFileList.java Purpose: Dumps all the Files and Folders from the location provided
Author : Anaparthi Subrahmanyam
Published: 2009-03-08
Usage Notification: Author doesnt own responsibility of any loss of data, responsibility for misuse or loss of any nature if this code is used. If possible, please provide link to this webpage
*/
import java.io.File;
public class FolderFileList
{
public static void main(String[] args)
{
String name = args[0];
File f = new File(name);
String fState = null;
try
{
if(!f.exists())
{
throw new Exception("Invalid location!!!");
}
if(f.isDirectory())
{
File[] listF = f.listFiles();
for (int i=0 ;i <>
{
fState = listF[i].isDirectory() ? "Folder":"File";
System.out.println(listF[i].getName()+"\t\t\t"+" === > "+fState);
}
}
else
{
System.out.println("Location provided is not a file directory.");
}
} catch (Exception e) { e.printStackTrace(); }
}
}
---------------------------

Provide the location as a command line argument as mentioned below(dont forget to compile):
prompt> java FolderFileList c:\folder_name

Monday, February 16, 2009

How to verify, input location is a File or Directory in Java

Java provides a Class called File, which is under the package java.io , contains all the sufficient methods(functions), which can be used to extract almost all the important properties of the Directory or a File. In the below example LocationVerification.java as well, i have used java.io.File class to know whether the input provided by the user is a File or Folder i.e. Directory.

LocationVerification.java
-------------------------
/*
  File   : LocationVerification.java
  Purpose: Verifies the input location is File or a Directory 
  Author : Anaparthi Subrahmanyam 
  Published: 2009-02-16
  Usage Notification: Author doesnt own responsibility of any loss of data, responsibility for misuse or loss of any nature if this code is used. If possible, please provide link to this webpage.
*/
import java.io.File;

public class  LocationVerification
{
  public static void main(String[] args) 
  {
    String name = args[0];
    File f = new File(name);
    try
    {
      if(!f.exists())
      {
         throw new Exception("Invalid location!!!");
      }
       if(!f.isDirectory())
      {
         System.out.println("Location provided is a not a file directory, its a File");
      }
      else
      {
        System.out.println("Location provided is a file directory.");
      }

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}
---------------------------------

How to execute the above file:

1. Compile it successfully
2. Execute it with providing a command line argument such as:

cmd_prompt> java LocationVerification c:\test\sample

If the third part of the above command is file then result will show  as Location is file, if its directory then result will show you a message saying its directory.




Sunday, February 15, 2009

How to pass file as input in java program and parsing it.

There are different ways we can pass the file path as a parameter to a java file and can be parsed. One of the very good class provided by Sun is java.util.Scanner class. This class provides sufficient methods to accpet file as an input parameter and also provides methods to parse it.Here I am demonstrating you two different ways of passing file to a java program as well as parsing it.
# FileScanner.java
------------------------------
/* File : FileScanner.java
Purpose: Accepts file name as input and prints the content after parsing.
Author : Anaparthi Subrahmanyam
Published: 2009-02-15
Usage Notification: Author doesnt own responsibility of any loss of data, responsibility for misuse or loss of any nature if this code is used. If possible, please provide link to this webpage
*/
import java.util.Scanner;

import java.io.File;
public class FileScanner{
public static void main(String[] args) {
// creating an scanner object to recieve the Input
System.out.println("Please enter the complete file name including absolute path");
Scanner in = new Scanner(System.in);
// Following two variable will be used later
Scanner sn =null;
File f = null;
// accepting user input
String fname = in.next();
System.out.println("File Name Entered : " + fname);
f = new File(fname);
try
{
if(!f.exists()) throw new Exception(" File does not exists!");
// Creating scanner object using constructior Scanner(File f)
// This obj will be parsed using methods provided by the Scanner Class
sn = new Scanner(f);
// Parsing and printing untill end of line of the file provided.
while(sn.hasNext())
{
System.out.println(sn.next());
}
} catch (Exception e) { e.printStackTrace(); }
}
}
--------------------------
Compile it and just execute the class file, once the file gets started executing, it will ask for the file name . Once you entered the file name, press enter. Complete file content will be displayed on console.

The other way of using the Scanner class is as follows:
/* File : FileScanner2.java
Purpose: Accepts file name as input and prints the content after parsing.
Author : Anaparthi Subrahmanyam
Published: 2009-02-15
Usage Notification: Author doesnt own responsibility of any loss of data, responsibility for misuse or loss of any nature if this code is used. If possible, please provide link to this webpage
*/import java.util.Scanner;
import java.io.File;
public class FileScanner2{
public static void main(String[] args) {
String fname = args[0];

// Following two variable will be used later
Scanner sn =null;
File f = null;
// accepting user input
String fname = in.next();
System.out.println("File Name Entered : " + fname);
f = new File(fname);
try
{
if(!f.exists()) throw new Exception(" File does not exists!");
// Creating scanner object using constructior Scanner(File f)
// This obj will be parsed using methods provided by the Scanner Class
sn = new Scanner(f);
// Parsing and printing untill end of line of the file provided.
while(sn.hasNext())
{
System.out.println(sn.next());
}
} catch (Exception e) { e.printStackTrace(); }
}
}
--------------------------

In the above program, pass the file name while executing the java class file such as :
cmd_prompt> java FileScanner2 c:\test\sample.txt
It will print the content of the file on the console.

Monday, January 12, 2009

How to accept inputs from user while running java program

There are different waysm through which we can pass input value while java program is running.In the below example, i have used java.util.Scanner class for the same.

One of the main reason for using Scanner class is, it actually allows user to pass primitive data types such as integers etc.

Here you dont have to pass the argument while call the java class file, here user will be asked to enter input when actually inputs are required.

I am explaining  in terms of layman point of view so that it will be easier to understand.

Here is the code:
JavaScanner.java
--------------------
import java.util.Scanner;

/*
Author: Anaparthi Subrahmanyam
Published: 01-13-2009
*/
public class  JavaScanner
{
  public static void main(String[] args) 
  {
    Scanner scanner = new Scanner(System.in);
    try
    {
      System.out.println("Value 1");
      int int1 = scanner.nextInt();
      System.out.println("Value 2");
      int int2 = scanner.nextInt();
      System.out.println("Sum");
      System.out.println("---------- Sum-------------");
      System.out.println(int1+int2);
      System.out.println("---------------------------"); 
    }
    catch (Exception e)
    {
      System.out.println("Error in input");
      e.printStackTrace();
    }
  }
}
--------------------



Sunday, January 4, 2009

Tips for String comparison in Java

Whenever we compare two strings objects in java, we normally consider both string arguments with qual importance and ignorance.I like to suggest one tip here which may be helpful for you in avoiding to generate a silly error, which looks terrible :)

I have two String objects as Str1 and Str2

Now i will compare two strings mentioned above for comparison, both may contain same value or may be having different values.

I have two options to write a conditional statement using equals opertator :

Type 1:
 if(Str1.equals(Str2))
  ...............
  ...............

Type 2:
 if(Str2.equals(Str1)) 
  ...............
  ...............

Which one you prefer.. i came across so many people explaining that its not that important to prefer which one i will opt for , as both produces same result.
Here i want to suggest you to analyze your code little bit, and reach a point where you can say, which String object cannot be null.If you found Str1 cannot be null , then use Str1.equals(Str2) , i.e. compare Str2 with Str1 , keeping Str1 at first place.
Reason for saying this is , if you are calling the equals method of the String object containing null value then it will through java.lang.NullPointerException.

So just by following a simple approache, you can avoid an exception.

There is another place where you can use this:
Whenever you are comparing a Constant String Object with a Dynamic String Object i.e. a string object which can contain different value while in the process of any kind. Please see the example mentioned below:-

String constant = "ENTRY_X";
Now i have a dynamic string object as dynamicStr, about whihc i am not sure, what type of value it can contain, then i will use:
if(constant.equals(dynamicStr))
........
.......

or 

if("ENTRY_X".equals(dynamicStr))
........
.......


This way i am avoiding the NullPointerException.
Its a very small tip, but i found this usefull information for all.
Please provide your comments if any.

If you liked my post, linking to this post will be highly appreciated.