java - Storing strings in arrays and splitting strings -
i'm having java issue on uni assignment. we've been given file has set of info listed such (there's more, formatting example):
57363 joy ryder d d c p h h c d 72992 laura norder h h h d d h h h 71258 eileen on c f c d c c c p
for life of me, can't work out how store in array, , need split because letters need converted number , averaged, need stored sec array.
i'm new-ish java, lot of types of things require import @ start of code unknown me, explaining code changes in replies appreciated. code have far follows:
import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class studentgpa_16997761 { public static void main(string[] args) throws filenotfoundexception { scanner kb = new scanner(system.in); //get file system.out.print("please come in name of file containing pupil information: "); string gradefile = kb.next(); scanner grades = new scanner(new file(gradefile)); if (new file(gradefile).exists()) { while (grades.hasnextline()) { system.out.println(grades.nextline()); } } //student identification number, first name, surname, 8 individual alphabetic characters represent //unit grades student. hence, each line of info in text file represents pupil , grades //they achieved in 8 units of study //need create array hold pupil info //need create array holds pupil id , gpa } }
i know works, system.out.println prints out lines expected them read, can't figure out how store them. think miiight able split working, that'll still need array/arraylist first...
you can split string array using delimiter. in example, if first , lastly names not contain spaces themselves, can following:
while (grades.hasnextline()) { string line = grades.nextline(); string[] parts = line.split(" "); // basics string id = parts[0]; string firstname = parts[1]; string lastname = parts[2]; // extract grades int size = parts.length - 3; string[] gradelist = new string[size]; system.arraycopy(parts, 3, gradelist, 0, size); // grades }
java arrays
No comments:
Post a Comment