java - Want to count characters from cmd argument array -
i'm quite new java. have studied code long time , can't find way declare args array in simple string can count _length();
i getting args array doing following in cmd: java sentence day die
. output way want atm, but, how efficiently count args array characters? way of declaring every single args[0], args[1]
, etc doesn't feel right.
right code:
public class sentence { public static void main(string[] args) { string s = args[0]; string t = args[1]; string = args[2]; string r = args[3]; string l = args[4]; int sum = s.length()+t.length()+a.length()+r.length()+l.length(); system.out.print("you wrote: "); (int j=0; j < args.length; j++){ system.out.print(" "+args[j]); } if (args.length > 0) { system.out.println("\nnumber of words:\t"+args.length); system.out.println("number of characters:\t"+sum); } } }
args
array. should iterate on :
int sum=0; (string s : args) { sum+=s.length(); }
Comments
Post a Comment