java - How do I read this text file into the following class array? -
i have class student , 2 children classes, casualstudent , permanentstudent. doing right now.
employee[] = new employee[10]; int count; a[0] = new permanentstudent("john", "lui", "hjfddfdfj"); a[1] = new permanentstudent("peter", "vamplew", "vam12345678"); a[2] = new permanentstudent("rudi", "skacel", "ska51515151"); a[3] = new casualstudent("katie","blackburn","bla41925612"); a[4] = new casualstudent("neal","stephenson","ste97527467"); a[5] = new casualstudent("neneh","cherry","che98765432"); a[6] = new casualstudent("chris","brookmyre","bro97635198"); a[7] = new casualstudent("grace","hopper","hop26554432"); a[8] = new casualstudent("randall","munroe","xkcd1234567"); a[9] = new casualstudent("kaylee","frye","fry90224718");
each of these children classes have constructor , have manually coded data in code. now, want read data .txt file. know how read , have done till now:
import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception; public class program { public static void main(string[] args) { bufferedreader reader = new bufferedreader(new filereader( "file.txt")); while (true) { string line = reader.readline(); if (line == null) { break; } system.out.println(line); } reader.close(); } }
but how read data classes this? new java , not able understand how this? please guide me in right direction. thanks!
also, text file contain 10 lines , each line have following format.
john lui hjfddfdfj ..................
code:
61. int = 0; 62. (i = 0; < 10; i++) 63. { 64. string x = a[i].tostring(); 65. system.out.println(x); 66. }
the simplest way follows:
employee[] employees = new employee[10]; try (bufferedreader reader = new bufferedreader(new filereader("file.txt"))) { (int = 0; < 3; i++) { string line = reader.readline(); string[] parts = line.split(" "); employees[i] = new permanentstudent(parts[0], parts[1], parts[2]) } (int = 3; < 10; i++) { string line = reader.readline(); string[] parts = line.split(" "); employees[i] = new casualstudent(parts[0], parts[1], parts[2]) } }
Comments
Post a Comment