java - Reading from a text file into an array -
hi im trying hackerearth challenge sum of medians , involves me reading text file , storing values in array. first value has stored in variable n able the remaining values have stored in array. become stuck. have read each value line line , store in array . code have been trying working on cant see im going wrong.
import java.io.bufferedreader; import java.io.inputstreamreader; class testclass { public static void main(string args[] ) throws exception { // read number of data system standard input. bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); string line = br.readline(); int n = integer.parseint(line); int = 1; int[] myintarray = new int[n]; // median sum long summedians = 0; int median = 0; while (i<n) //read 1 line file , parse integer //store value in array { myintarray [i] = integer.parseint(line); = + 1; // increment total numbers read }
so said must increment through text file storing each value on line in array. amazing
the text file
5
10
5
1
2
15
one string per line, have pass integer. doing after store value line array sorting , finding medium , repeat process until values text file have been read.
the problem trying 1
http://www.hackerearth.com/problem/algorithm/sum-of-medians-1/
if you're reading text file (and not standard input you're doing @ moment) want like:
// warning: fail if filename invaild. bufferedreader br = new bufferedreader(new filereader("inputfilename.txt"));
to read in each line, can use following in while loop:
// warning: crash program if line contains other integers. myintarray[i] = integer.parseint(br.readline()) = + 1; // increment total numbers read
you should close reader @ end:
try{ br.close(); } catch (ioexception e) { system.out.println("error, program exit!"); system.exit(1); }
the import should swapped import java.io.inputstreamreader
to: import java.io.filereader
Comments
Post a Comment