Thursday 15 May 2014

java - Count the length of an array -



java - Count the length of an array -

i having input file first line size of array(n) sec line n element

i have proceeded if n ==no of element in sec line ex:

3 1 3 4 //ok 4 3 5 6 7 7 // nil

my code:

scanner in = new scanner(new filereader("ok.txt")); int n = in.nextint(); if(n==in.nextline().length()) // create array of element in.nextline().length() not giving me right length i.e 3 , 5

in.nextline() give string containing :

"1 3 4"

with 2 spaces , 3 digits. should :

string line = in.nextline(); string[] digits = line.split(" "); if (n==digits.length) { ...

moreover aware of fact using in.nextline() in if condition, leads lost content of line

java

No comments:

Post a Comment