Write a program that accepts up to six arguments at the command line prompt. The
ID: 3672190 • Letter: W
Question
Write a program that accepts up to six arguments at the command line prompt.
The program should print the first character of any oddnumbered
arguments (numbers 0, 2, and 4), and the second character of any
even-numbered arguments (numbers 1, 3, and 5). The characters printed should
be separated by spaces. The program should inform the user of the correct program
usage if fewer than two or more than six arguments are provided. Assume
each argument contains at least two characters. For example:
ahoover@video> myprog arg1 200 list all arg5
m r 2 i a r
ahoover@video>
Explanation / Answer
class cmd_line_6
{
public static void main(String args[])
{
int n=args.length;
char c='a';
if(n<2 || n>6)
{
System.out.println
("Command Line Arguments not correct");
return;
}
for(int i=0;i<6;i++)
{
if(i%2==0)
c=args[i].charAt(1);
else
c=args[i].charAt(0);
System.out.print(c+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.