JetBrains Academy: Command line arguments - Kamil-Jankowski/Learning-JAVA GitHub Wiki
JetBrains Academy: Command-line arguments
Parameters and their values:
Write a program that takes command-line arguments in the format: parameter1 value1 parameter2 value2 ...
and outputs them in the standard output in the format:
parameter1=value1 parameter2=value2 ...
It is guaranteed, that each parameter is followed by a value.
class Problem {
public static void main(String[] args) {
for (int i = 0; i < args.length; i+=2){
System.out.println(args[i]+"="+args[i+1]);
}
}
}
The index of an argument:
Write a program that searches the argument equal to "test" (without quotes), and then outputs its index in the array args. If it is not found, the program must output "-1".
class Problem {
public static void main(String[] args) {
int index = -1;
for (int i = 0; i < args.length; i++){
if (args[i].equals("test")){
index = i;
break;
}
}
System.out.println(index);
}
}
Calculator - 1:
Write a program that takes an operator ("+", "-", "*") and two values as the command-line arguments and then outputs the result of the operator in the standard output. If the passed operator is not from the list, it must output the string "Unknown operator" without quotes.
class Problem {
public static void main(String args[]) {
int result = 0;
if (args[0].equals("+")){
result = Integer.parseInt(args[1]) + Integer.parseInt(args[2]);
System.out.println(result);
} else if (args[0].equals("-")){
result = Integer.parseInt(args[1]) - Integer.parseInt(args[2]);
System.out.println(result);
} else if (args[0].equals("*")){
result = Integer.parseInt(args[1]) * Integer.parseInt(args[2]);
System.out.println(result);
} else {
System.out.println("Unknown operator");
}
}
}
Calculator - 2:
Write a program that takes an operator ("MAX", "MIN", "SUM") and a sequence of numbers as the command-line arguments and then outputs the result of the operator in the standard output.
The description of the operators:
- "MAX" finds the max value of a sequence of numbers;
- "MIN" find the min value of a sequence of numbers;
- "SUM" calculates the sum of a sequence of numbers.
It is guaranteed that a correct operation and at least one number passed.
import java.util.*;
class Problem {
private static int sum(int[] arr){
int sum = 0;
for (int n : arr){
sum += n;
}
return sum;
}
public static void main(String[] args) {
int[] numbers = new int[args.length-1];
for (int i = 1, j = 0; i < args.length; i++, j++){
numbers[j] = Integer.parseInt(args[i]);
}
Arrays.sort(numbers);
if (args[0].equals("MAX")){
System.out.println(numbers[numbers.length-1]);
} else if (args[0].equals("MIN")){
System.out.println(numbers[0]);
} else if (args[0].equals("SUM")){
System.out.println(sum(numbers));
}
}
}
or:
class Problem {
public static void main(String[] args) {
int[] numbers = new int[args.length-1];
for (int i = 1; i < args.length; i++){
for (int j = i-1; j < i; j++){
numbers[j] = Integer.parseInt(args[i]);
}
}
if (args[0].equals("MAX")){
System.out.println(max(numbers));
} else if (args[0].equals("MIN")){
System.out.println(min(numbers));
} else if (args[0].equals("SUM")){
System.out.println(sum(numbers));
}
}
private static int max(int[] arr){
int max = arr[0];
for (int n : arr){
if (n > max){
max = n;
}
}
return max;
}
private static int min(int[] arr){
int min = arr[0];
for (int n : arr){
if (n < min){
min = n;
}
}
return min;
}
private static int sum(int[] arr){
int sum = 0;
for (int n : arr){
sum += n;
}
return sum;
}
}