Wednesday, April 13, 2011

Command Line Input in JAVA

java tutorial
In this tutorial we will see techniques how can we pass input to a JAVA program via command lime or shell prompt. There are many ways in which we can pass input to our JAVA program i.e.
  • we can pass arguments when we initialize out program
  • we can use BufferedReader Class to take input
  • we can also use Scanner Class for the said purpose
and may others too, usually we use these mentioned above...

Now, Let the Code Talk

to pass argument at program initialization... suppose the below code except arguments
public class Echo {
    public static void main (String[] commandLineArguments) {
        for (String singleValue: commandLineArguments) {
            System.out.println(singleValue);
        }
    }
}
first compile the program and from console write
java Echo Java is Cool
the output will be
Java
is
Cool
Please Note arguments are space delimited and can be off any number

to pass argument at runtime we use BufferReader Class
import java.io.*;

public class Echo {
   public static void main (String[] nix) {

      //  prompt the user to enter their name
      System.out.print("Enter your name: ");

      //  open up standard input
      InputStreamReader stdIn=new InputStreamReader(System.in);
      BufferedReader buffRead = new BufferedReader(stdIn);

      String userName = null;

      //  read the username from the command-line;
      //  need to use try/catch with the
      //  readLine() method
      try {
         userName = buffRead.readLine();
      } catch (IOException ioe) {
         System.out.println("Error Occured"+ioe.getMessage());
         System.exit(1);
      }
      System.out.println("Thanks for the name, " + userName);
   }
}
here we readLine() Method, but we can also use read(); i read single character.

take input from console using Scanner Class, it provide rich set of methods to take input
import java.util.Scanner;

public class Echo {
    public static void main(String[] nix) {
        
        Scanner scanner = new Scanner(System.in);

        //
        // Read string input for username
        //
        System.out.print("Username: ");
        String username = scanner.nextLine();

        //
        // Read string input for password
        //
        System.out.print("Password: ");
        String password = scanner.nextLine();

        //
        // Read an integer input for another challenge
        //
        System.out.print("What is 2 + 2: ");
        int result = scanner.nextInt();

        if (username.equals("Originative") 
                && password.equals("tutorialjinni")
                && result == 4) {
            System.out.println("Welcome to Java Application");
        } else {
            System.out.println("Invalid username or password, access denied!");
        }
    }
}
Scanner has rich set of methods refer documentation for it.

0 comments:

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems