java - Why is my array still filled with 7 elements instead of 6? -
i have text file being read , filled 3 arrays supposed of size 7 each. i'm testing see if program recognize there 6 elements if delete number text file when print out array lengths each array still come out @ length 7 , i'm not sure why.
public static void main(string[] args) throws nosuchelementexception { string file = "input.txt"; string text = ""; double [] breakfast = new double [7]; double [] lunch = new double [7]; double [] dinner = new double [7]; try { scanner s = new scanner(new file(file)); (int = 0;i<7;i++) { breakfast[i] = s.nextint(); lunch[i] = s.nextint(); dinner[i] = s.nextint(); } } catch(nosuchelementexception n) { } catch(filenotfoundexception e) { system.out.println("file not found"); } if (breakfast.length != 7 || lunch.length != 7 || dinner.length != 7) { system.out.println("your file not work program. please select another."); system.exit(0); } system.out.println(breakfast.length); system.out.println(lunch.length); system.out.println(dinner.length);
no numbers missing text file:
800 1000 800 450 845 1200 1800 250 400 0 1500 1800 600 500 1000 700 1400 1700 675 400 900
one number missing text file:
800 1000 800 450 845 1200 1800 250 400 0 1500 1800 600 500 1000 700 1400 1700 400 900
whether modify elements of collection, collection still size initialized to.
if initialize new array size 7, hold default value of double: [0, 0, 0, 0, 0, 0, 0]. size final, , can change value of each element, not existence. cannot remove element unless re-declare entire array new size, , copy on elements.
Comments
Post a Comment