Parse an input file into two separate files in java -
i have input file below (for example):
10 12 1 2 ... 9 10 1 2 ... 11 12
the first tells next 10 lines part1
then following next 12 lines part2
.
i create 2 separate files part1.txt
, part2.txt
parsing original input.txt
file.
how can that? suggestions kindly? using java scanner.
solution (partial): worked me based on suggestions below
scanner scanner = new scanner(filename); try { string[] first_line = scanner.nextline().split("\\s+", 3); // reading first line of input file int edgecount = integer.parseint(first_line[0]); int vertexcount = integer.parseint(first_line[1]); string hasweight = first_line[2]; while (scanner.hasnextline()) { if(edgecount != 0) { // check whether more edges left read input file scanner edge_scanner = new scanner(scanner.nextline()); ....
since sounds homework, not go code detail, read first line, , use .split("\\s+")
method string class.
once this, end array 10
in first location , 12
in second.
while iterate on next lines, keep counter , check if value of counter less or equal 10. if holds, know need output 1 file. if condition no longer holds , counter greater 10
less or equal 10 + 12
, know should print in second file.
Comments
Post a Comment