How would I print out certain values of an Array with booleans in Java? -
i made program prints out manipulated arrays, reprint values in pass[i] array has length equal 7 java seems reprint entire set. guessing doing wrong handling of booleans...
string[] pass = new string[cnt]; int[] range = new int[cnt]; int[] arr = new int[cnt]; boolean valid; (int = 0; < cnt; i++) { pass[i] = afname[i] + asname[i]; if (pass[i].length() == 7) { system.out.println(pass[i]); valid = true; } else { valid = false; if ((7 - pass[i].length()) >= 3) { range[i] = (int) math.pow(10, (7 - pass[i].length())) - 100; system.out.println(pass[i] + range[i]); } else { if ((7 - pass[i].length()) == 2) { range[i] = 99; system.out.println(pass[i] + range[i]); } else { range[i] = 9; system.out.println(pass[i] + range[i]); } } } } if(valid){ for(int i=0; i<cnt; i++){ system.out.println(pass[i]); } } else{system.out.println("cannot print");}
output without if(valid) statement...
jbeebee span900 baroura apentz9 swillia ajenn99
output if(valid) statement...
jbeebee span900 baroura apentz9 swillia ajenn99 cannot print
the problem valid
true
after loop if last element of array has 7 symbols. if valid
true
after loop print entire array. understand question need print strings 7 symbols.
one way create array (or list
) store string 7 symbols.
string[] pass = new string[cnt]; int[] range = new int[cnt]; int[] arr = new int[cnt]; list<string> valids = new arraylist<>(); (int = 0; < cnt; i++) { pass[i] = afname[i] + asname[i]; if (pass[i].length() == 7) { system.out.println(pass[i]); valids.add(pass[i]); } else { if ((7 - pass[i].length()) >= 3) { range[i] = (int) math.pow(10, (7 - pass[i].length())) - 100; system.out.println(pass[i] + range[i]); } else { if ((7 - pass[i].length()) == 2) { range[i] = 99; system.out.println(pass[i] + range[i]); } else { range[i] = 9; system.out.println(pass[i] + range[i]); } } } } if(valids.isempty()){ system.out.println("no entries length of 7"); } else { system.out.println("entries length of 7:"); for(string s : valids){ system.out.println(s); } }
another create boolean
array , mark elements true
if corresponding element of pass
has required length.
also can make check of length without valid
variable.
for(string s : pass){ if(s.length() == 7) { system.out.println(s); } }
Comments
Post a Comment