X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Command Line Arguments in Java :

The values which are send from command prompt to main() method are called command line arguments.

These arguments are of String type.

Example 1 :

class Example { public static void main(String [ ] args) { System.out.println(args[0] + args[1]); } }

Output :

// save as : Example.java // compile as : javac Example.java // run as : java Example 10 20 1020

Example 2 :

class Example { public static void main(String [ ] args) { System.out.println(args.length); for(String s : args) System.out.println(s); } }

Output :

// save as : Example.java // compile as : javac Example.java // run as : java Example Algo Valley 2 Algo Valley

Space is the separator between command line arguments and if the command line argument itself contain space then we should enclosed it with double quotes (" ").

Example :

class Example { public static void main(String [ ] args) { System.out.println(args[0]); } }

Output :

// save as : Example.java // compile as : javac Example.java // run as : java Example "Algo Valley" Algo Valley

Note :

The maximum allowed number of command line argument is 2147483647 and minimum number of command line argument is 0.


Integer.parseInt() :

  • Integer is a predefined class available in java.lang package.

  • parseInt is the static method of Integer class.

  • This method converts String representation of integer to int.

Example :

class Example { public static void main(String [ ] args) { int sum = 0; for(String s : args) { sum = sum + Integer.parseInt(s); } System.out.println("Sum : " + sum); } }

Output :

// save as : Example.java // compile as : javac Example.java // run as : java Example 10 20 Sum : 30

System.console() :

  • console() is a static method of System class.

  • This method returns reference of console object.

  • This feature is added in Java 6.0

Example 1 :

class Example { public static void main(String [ ] args) { System.console().printf("Hello Java"); } }

Output :

// save as : Example.java // compile as : javac Example.java // run as : java Example Hello Java

Example 2 :

import java.io.*; class Example { public static void main(String [ ] args) { Console out = System.console(); out.printf("Hello Java"); } }

Output :

// save as : Example.java // compile as : javac Example.java // run as : java Example Hello Java

readLine() :

It is a method of Console class which read String.

If we want to read the data from keyword :

  • In C - scanf()

  • In Java - System.console().readLine()

Example 1 :

import java.io.*; class Example { public static void main(String [ ] args) throws IOException { // create a BufferedReader using System.in BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str; System.out.println("Enter a string :"); str = in.readLine(); System.out.println("Entered string :"); System.out.println(str); } }

Output :

// save as : Example.java // compile as : javac Example.java // run as : java Example Enter a string : USA Entered sting : USA

Example 2 :

import java.io.*; class Example { public static void main(String [ ] args) throws IOException { // create a BufferedReader using System.in BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number :"); int a = Integer.parseInt(in.readLine()); System.out.println("Entered number :"); System.out.println(a); } }

Output :

// save as : Example.java // compile as : javac Example.java // run as : java Example Enter a number : 123 Entered number : 123


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.