Command line arguments in Java to add two numbers

Daphne 145 Published: 10/18/2024

Command line arguments in Java to add two numbers

Here's an example of how you can use command-line arguments in Java to add two numbers:

Let's say we have a Java program called AddNumbers.java that takes two command-line arguments, representing the two numbers to be added, and prints their sum.

Here's the code:

public class AddNumbers {

public static void main(String[] args) {

if (args.length != 2) {

System.out.println("Usage: java AddNumbers ");

return;

}

int num1 = Integer.parseInt(args[0]);

int num2 = Integer.parseInt(args[1]);

int sum = num1 + num2;

System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);

}

}

Let's break it down:

The main method takes an array of Strings, args, which represents the command-line arguments passed to the program. We check if the number of command-line arguments is not equal to 2. If it's not, we print a usage message and return from the method. If the number of command-line arguments is 2, we parse each argument as an int using Integer.parseInt. This will throw a NumberFormatException if the argument cannot be parsed as an int. We calculate the sum of the two numbers and store it in the sum variable. Finally, we print out the sum to the console.

To run this program from the command line, you would do something like:

java AddNumbers 10 20

This would output:

The sum of 10 and 20 is: 30

Note that if you pass fewer or more than two command-line arguments, the program will print a usage message and exit.

Also, if you try to pass non-numeric values as command-line arguments, the program will throw a NumberFormatException when trying to parse them as ints.

This is just one example of how you can use command-line arguments in Java. The possibilities are endless!

How to use Java command prompt?

I'd be happy to help! However, I must correct you - there is no such thing as a "Java command prompt." The Java programming language is used for developing software, and it doesn't have its own built-in command-line interface or shell.

What you're probably thinking of is the Java Development Kit (JDK) that comes with the Java Runtime Environment (JRE). The JDK includes various tools for compiling, running, and debugging Java programs. One such tool is called javac (short for "Java compiler"), which can be used from a command-line interface.

To use javac from the command line, you'll need to:

Open your operating system's terminal or command prompt. Navigate to the directory where your Java source files are located using the cd command. Use javac to compile your Java program by typing the following command: javac YourProgram.java This will generate a .class file with the same name as your Java source file (e.g., YourProgram.class). To run your Java program, use the java command followed by the class name: java YourProgram

Here's an example of how this might work:

Suppose you have a Java program called HelloWorld.java in a directory called C:JavaProjects. You can compile it using the following command:

cd C:JavaProjects

javac HelloWorld.java

This will generate a file called HelloWorld.class in the same directory. To run the program, you'd use the following command:

java HelloWorld

This would print "Hello, world!" to the console.

In addition to javac, other useful tools that come with the JDK include:

javadoc: generates HTML documentation for your Java programs. jar: creates a JAR (Java Archive) file containing your compiled Java classes and any dependent libraries. jps and jstack: tools for monitoring and debugging Java processes.

I hope this helps! Let me know if you have any further questions.