java - How to create multiple arrays from one text line? -


i'm first year programming student, forgive me if dumb question.

what i'm trying read text file contains answer key on line 1, , proceeding lines contain student's id number , answers test. here example:

tfftffttttfftftftftt abc54301 tftftfttttf tftftftt abc54302 tfftfffffffftftftftt abc54303 tfftffttttfftftttttt abc54304 tfftttffttfftftftftt abc54305 tftftfttttfftftftftt 

my idea create 3 arrays - 1 contains answer key, 1 contains students' id numbers, , 1 contains studentss answers. student id number 8 digits long, can possibly contain spaces. student's answers 20 characters long, can contain spaces. can't split them based on spaces.

the instructions contained this:

 while (infile.hasnext())  {    ...  }  'infile' scanner object connected input file.  each student's record should read 2 read's --  1 string (for id) , string (answer string). 

i'm pretty lost. initial idea loop through each line past first, , set each segment of line complimentary array. problem i'm reaching can't seem find instruction or clarification on how split lines based on number of characters.

i thinking maybe like:

 while (infile.hasnext())  {     // somehow set first line own array     // loop continues, long not first line     idnums[i] =     //characters 1-8     studentanswers[i] =     //characters 10-30  } 

is reasonable? or there easier way? feel i'm approaching in manner more complicated necessary.

edit:

after learning more maps , regex, i've attempted several different versions of code. first consisted of:

while (scan.hasnextline()) {     string[] studentinfo = scan.nextline().split(" ", 2);     info.put(studentinfo[0], studentinfo[1]); } 

which kind of worked, problem if ended spaces, wouldn't save full 20 characters. issue? i'm not sure. i'm testing this:

string[] answerkey = scan.nextline().split("");  map<string, string[]> results = new hashmap<>(); while (scan.hasnextline()) {     string line = scan.nextline();     results.put(line.substring(0, 8), line.substring(9).split("")); } 

which giving me correct keys, incorrect values. values seem pointing memory locations. example:

{abc54339=[ljava.lang.string;@55f96302 

since instructions explicitly should make 2 reads, best way i've found after reading this documentation fulfill instructions scanner.next(string)

it looks "pattern" parameter expects regular expression, http://rubular.com/ great resource experimenting regular expressions, , has reference basic regex syntax.

for convenience, regular expression matching 8 characters ".{8}"

if use regular expression, make sure escape \s use regex escape sequence(on escape java , 1 regex language), match numeric digit, have use "\\d"


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -