0

I have the input as 07/12 on my command line how can I use this two argument as args[0] and args[1] to convert into month and day

int month = Integer.parseInt(args[0]);
int day = Integer.parseInt(args[1]);

The error I am having is "Exception in thread "main" java.lang.NumberFormatException: For input string: "07/12"

K. Ji
  • 55
  • 9
  • 1
    Well you can't use that as two arguments, because it's one argument. Either you need to split that one argument into two strings (e.g. with `String.split`) or provide two arguments on the command line instead. – Jon Skeet Mar 10 '17 at 16:00
  • Yes, there's no magic parsing of CLI arguments, use a split or a scanner here –  Mar 10 '17 at 16:00
  • So you typed in 07/12 and it's taking that whole thing as arg[0] but failing to convert it to an int. – Alex Mar 10 '17 at 16:00

2 Answers2

6

It's because the date you are passing is all contained in the args[0]. Try this

String[] date = args[0].split("/");
int month = Integer.parseInt(date[0]);
int day = Integer.parseInt(date[1]);
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
1

It's looking at the whole string. You want to split the string then parseInteger.

HSchmale
  • 1,838
  • 2
  • 21
  • 48