i attempting create rather large program. 1 of things need program read text file , use fill in values multiple objects of array. in case of program post, each object of array has 3 different data types need fill in values having program read text file , pull out correct pieces of data each line. know how program read text file , print out data line line, not know how create loop pick out specific pieces of data line in text file , put them in appropriate places each object. following program simplified version of larger program complete. on matter appreciated.
package clonecounter; import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class clonecounter { public string name; public int age; public double weight; clonecounter(string clonename, int timespentliving, double heaviness) { name = clonename; age = timespentliving; weight = heaviness; } public static void main(string[] args) { clonecounter [] clone = new clonecounter[3]; //this textfile looks like. //billy 22 188.25 //sam 46 301.77 //john 8 51.22 //code can read , print textfile string filename = "data.txt"; scanner inputstream = null; system.out.println("the file " + filename + "\ncontains following lines:\n"); try { inputstream = new scanner(new file("data.txt"));//the txt file being read correctly. } catch(filenotfoundexception e) { system.out.println("error opening file " + filename); system.exit(0); } list<clonecounter> clones = new arraylist<clonecounter>(); while (inputstream.hasnextline()) { string line = inputstream.nextline(); string[] data = line.split(" "); clonecounter clone = new clonecounter(data[0],integer.parseint(data[1]),double.parsedouble(data[2])); clones.add(clone); } } inputstream.close(); //temporary placeholders fill in values objects until can figure out how import , implement data text file clone[0] = new clonecounter ("billy", 22, 188.25); clone[1] = new clonecounter ("sam", 46, 301.77); clone[2] = new clonecounter ("john", 8, 51.22); for(int i=0; i<3; i++) { system.out.println(clone[i].name + " " + clone[i].age + " " + clone[i].weight); } } }
if data separated single space can following:
list<clonecounter> clones = new arraylist<clonecounter>(); while (inputstream.hasnextline()) { string line = inputstream.nextline(); string[] data = line.split(" "); clonecounter clone = new clonecounter(data[0],integer.parseint(data[1]),double.parsedouble(data[2])); clones.add(clone); }
edit: here full program copy , paste , work fine (tested):
package clonecounter; import java.io.file; import java.io.filenotfoundexception; import java.util.arraylist; import java.util.list; import java.util.scanner; public class clonecounter { public string name; public int age; public double weight; clonecounter(string clonename, int timespentliving, double heaviness) { name = clonename; age = timespentliving; weight = heaviness; } public static void main(string[] args) { //this textfile looks like. //billy 22 188.25 //sam 46 301.77 //john 8 51.22 //code can read , print textfile string filename = "data.txt"; scanner inputstream = null; system.out.println("the file " + filename + "\ncontains following lines:\n"); try { inputstream = new scanner(new file("data.txt"));//the txt file being read correctly. } catch(filenotfoundexception e) { system.out.println("error opening file " + filename); system.exit(0); } list<clonecounter> clones = new arraylist<clonecounter>(); while (inputstream.hasnextline()) { string line = inputstream.nextline(); string[] data = line.split(" "); clonecounter clone = new clonecounter(data[0],integer.parseint(data[1]),double.parsedouble(data[2])); clones.add(clone); } inputstream.close(); for(int i=0; i<3; i++) { system.out.println(clones.get(i).name + " " + clones.get(i).age + " " + clones.get(i).weight); } } }
Comments
Post a Comment