Function that checks Java program arguments -
i'm trying re-write code in cleaner way:
public static void main(string[] args) { if(checkargs(args) < 0) return; return; } private static int checkargs(string[] args) { if (args.length == 0) { system.out.println("error: no args"); return -1; } try{ //if it's number int mynumber = integer.parseint(args[0]); system.out.println("my number is: " + mynumber); } catch (exception e) {//if it's string try{ string[] mystr = args ; system.out.print("my string "); (int i=0; i<args.length; i++) { mystr[i] = args[i]; system.out.print(mystr[i]); } system.out.print("\n"); return 0; } catch(exception err){ system.out.println("error"); return -1; } } return 0; }
the code checks program args , tell user if it's string or number.
any ideas on how re-write code without using try-catch?
first, don't need second try...catch
. code:
string[] mystr = args ; system.out.print("my string "); (int i=0; i<args.length; i++) { mystr[i] = args[i]; system.out.print(mystr[i]); } system.out.print("\n"); return 0;
is not throwing checked exception , not ever throw runtime exception.
second, code has parts not needed - mystr
array - assign args
array it, , assign each element individually it. , it's local variable it's going go away return
anyway.
so:
system.out.print("my string "); (int i=0; i<args.length; i++) { system.out.print(args[i]); } system.out.print("\n"); return 0;
would same thing , not going throw exceptions.
now, checking integer - think using , catching exception enough. catch numberformatexception
instead of exception
. it's never idea catch-all.
but if insist on method without try-catch, this:
private static boolean checkint(string str) { if ( ! str.matches("-?0*[0-9]{1,10}")) { return false; } long l = long.valueof(str); if ( l > integer.max_value || l < integer.min_value) { return false; } system.out.println("my number is: " + mynumber); return true; }
this first checks have no more 10 digits or without minus sign (and number of preceding zeros). if so, can safely converted long
, without exception checking. can check if resulting long
in range of integer. it's workaround requires no try , catch, don't think it's better try-catch method.
so, try-catch, have:
private static int checkargs1(string[] args) { if (args == null || args.length == 0) { system.out.println("error: no args"); return -1; } try { // if it's number int mynumber = integer.parseint(args[0]); system.out.println("my number is: " + mynumber); } catch (numberformatexception e) {// if it's string string[] mystr = args; system.out.print("my string "); (int = 0; < args.length; i++) { mystr[i] = args[i]; system.out.print(mystr[i]); } system.out.print("\n"); return 0; } return 0; }
and without it, using checkint()
method:
private static int checkargs2(string[] args) { if (args == null || args.length == 0) { system.out.println("error: no args"); return -1; } if ( ! checkint(args[0])) { // if it's string string[] mystr = args; system.out.print("my string "); (int = 0; < args.length; i++) { mystr[i] = args[i]; system.out.print(mystr[i]); } system.out.print("\n"); return 0; } return 0; }
i'm not sure how useful check is, though. usually, argument checking done in preparation doing args, , don't save information whether number or string anywhere.
Comments
Post a Comment