java - Troubles with splitting and parsing arguments to the main() method -
i'm having difficulties output main method. if used enters bunch of random strings, program should integers , group them pair. example, if user enters 3 2 54 -5
, output should be:
(3,2) (54,-5)
or, example: if input 1 2 3
, program should output
(1,2)
because there not other pair found number 3
. main point of program gather numbers pairs. exception thrown if program cannot convert string int. smb please me out?
public static void main(string [] args) throws exception { int [] number = new int [args.length]; try { (int = 0; < args.length; i++) { number[i] = integer.parseint(args[i]); system.out.println("("+i+",")); } } catch (numberformatexception e) { system.out.println(e.getmessage()); } }
i think can use split method string class if know user use format, cannot parse them because user entered spaces. if data user entered comes in string array can use like:
public static void main(string [] args) throws exception { int[] numbers = new int[args.length]; try { (int = 1; < args.length; i+=2) { number[i-1] = integer.parseint(args[i-1].split(" ")[0]; number[i] = integer.parseint(args[i].split(" ")[0]; system.out.println("("+number[i-1]+","+number[i]+")"); } } catch (numberformatexception e) { system.out.println(e.getmessage()); } }
also if use i+=2, can see in for, start in 1 , goes 2 2. works , previous number, guaranteeing there pairs
Comments
Post a Comment