How do I print out a set of integers from my args
variable in Java?
I tried:
System.out.println("The numbers are " + args.length);
But all that does is print out the number of elements in the array.
I want it so that if there are 5 arguments passed: 1 2 3 4 5
the output should be:
1, 2, 3, 4, 5
Take a look if you like
System.out.println("The numbers are "+Arrays.toString(args));
If not, you'll have to loop over the array and format it yourself.
But all that does is print out the numbers in the array not the numbers separately.
This seems to be somewhat unclear.
args.length
gives the length of the array rather than its elements.
Use a for loop:
System.out.println("The numbers are ");
for(int i=0; i < args.length; i++)
System.out.print(args[i] + " ");
Try:
for(String arg: args)
System.out.print(arg + " ");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.