Untitled
unknown
plain_text
4 years ago
1.4 kB
18
Indexable
import java.io.*;
public class BananaCoconut {
public static void main(String args[]) {
try {
int count = args.length; // Get number of command line arguements.
if (count == 0) // Throw error message if zero command line arguements are given.
throw new NumberFormatException();
int array[], i = 0;
array = new int[count];
for (i = 0; i < count; i++) {
array[i] = Integer.parseInt(args[i]); // Parsing the string command line arguements to integers.
}
for (i = 0; i < count; i++) {
//Condition checks over the parsed command line arguements.
if (array[i] < 1)
System.out.print("puttputt");
else if (array[i] % 3 == 0 && array[i] % 7 == 0)
System.out.print("banana-coconut");
else if (array[i] % 3 == 0)
System.out.print("banana");
else if (array[i] % 7 == 0)
System.out.print("coconut");
else
System.out.print(array[i]);
if(i != count-1)
System.out.print(" ");
}
} catch (NumberFormatException e) {
// Error message if command line arguements are not valid or zero in number.
System.err.println("One or more numbers required as a command line argument." +
'\n' + "Example Usage: java BananaCoconut [number] [number] [...]");
}
}
}Editor is loading...