- 1

Please cheak code and tell me error

class programn { public static void main(String args[]) { int i=Integer.parseInt(args[0]); System.out.println("input"+i); } }

24th Sep 2017, 5:18 AM
Coding Beast
Coding Beast - avatar
8 Answers
+ 2
public class Program { public static void main(String[] args) { if(args.length > 0) { int i=Integer.parseInt(args[0]); System.out.println("input"+i); } else { System.out.println("No argument given"); } } }
24th Sep 2017, 5:42 AM
ChaoticDawg
ChaoticDawg - avatar
+ 13
When nothing is passed as argument to main(), accessing args[0] will cause index out of bounds exception.
24th Sep 2017, 5:29 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
If you run the program without any additional command line arguments then you will get an array index out of bounds exception. So you may want to add a check for the length of the args array.
24th Sep 2017, 5:31 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
If you're running it in the playground you won't be able to add the argument. You'd need to run it via the command line on your computer or on another online ide that allows you to add arguments.
24th Sep 2017, 5:39 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
oh now i understand... thank you guyz for your invaluable help... thatz mean playground does not support command line argument..
24th Sep 2017, 5:42 AM
Coding Beast
Coding Beast - avatar
0
what should i do now to remove error??
24th Sep 2017, 5:35 AM
Coding Beast
Coding Beast - avatar
0
Insert an argument.
24th Sep 2017, 5:36 AM
Emilio
Emilio - avatar
0
where did i have to inseart argument?šŸ˜¢
24th Sep 2017, 5:38 AM
Coding Beast
Coding Beast - avatar