Question:
Java: populate an array of objects, each with multiple parameters, from a file?
M47R1X
2011-06-16 07:31:16 UTC
Well I'm in a pickle. I'm trying to make a program here that can read from a text file, populate a number of objects (one per line of the file), and assign data from the file to variables within the objects (a little bit of everything here, not just one type of variable, but each line has the same).

The text file has a SET number of lines (I'm using 8 for now, so an array of 8 objects is needed, and I'd like to be able to change it in the future without being a huge headache, so it really needs to have objects). Each line has a boolean, two doubles, two strings, and an int in that order. There is not always a space between numbers and letters, but ALL strings are letters-only. I'm pretty new, but trying to get the hang of this since my class I had for Java this last semester. I'd rather use arrays for the objects to keep the program nice and clean, but I'm open to any suggestions.

Here's an example of what's in the file:

false 98.0 78.1Fall Cable 65
true 105.2 55.0Spring Satellite 804
...
...

And it goes on for eight lines.

Some names that would work for the variables are:

boolean basic, double west, double east, String season, String service, int channel

A fairly quick pointer would be appreciated, but I am not in a rush. I learn more from pointers that just giving me the corrected code, which is why I'm not putting anything up yet, save for two random lines of the text. Thanks!

AND NO, I'M NOT GOING TO WASTE MY TIME AS A HACKER. I want to go on to develop video games. Thanks!
Three answers:
Jim Maryland
2011-06-16 08:21:36 UTC
Reading the file into a String is something that you can easily find examples for online so I'll leave that for you to locate. As for parsing the String into individual components, that's going to be a bit of a pain since you don't always have a delimiter and the lines aren't a fixed format. You'll probably need to do things like:



// readline = (a line from the file)



String boolStr = readline.substring(0, readline.indexOf(" ")); // gets the true/false characters



You'll need to use similar techniques to get the rest of the values from the line read from the file (in fact it might make sense to update the readline by chopping off whatever you read into the variables).



After you get the parsing worked out, I'd recommend working with a templated array list. First, create your object bean to hold the data from the line:



public class MyDataBean {



// private variables

private boolean basic = false;

private double west = 0.0;

private double east = 0.0;

private String season;

private String service;

private int channel = 0;



// Now to have setter/getters (you'll need for every private variable)



public void setBasic(boolean basic) {

// Stores the supplied value in the private variable

this.basic = basic;

}



public boolean getBasic() {

// returns the private variable value

return this.basic;

}



// Do similar for the other fields.



}





Now when you read the values, you'll create a MyDataBean object with the value and add it to an array list:



// likely to be in a loop, but just showing the basics here



MyDataBean myDataBean = new MyDataBean()



// Now populate it

myDataBean.setBasic();

myDataBean.setWest();

// and so on to fill in all the values



Now you'll have an array list



List myDataBeanList = new ArrayList();



myDataBeanList.add(myDataBean);





So your "myDataBeanList" is your array that is templated to hold the bean object with your values. The List is like an array but more flexible. It will grow as needed if you need to add more values.
deonejuan
2011-06-16 08:11:25 UTC
Java has java.utility.Scanner; That is one way. You move through the incoming data looking for nextInt(), nextDouble();



The other thing you can do is read a line of text at a time and then process it. We can make a sized array to grab the fields with String.split( "\t" ); in this case the tab character will split the line into array members. Essentially, this is what we did in the mainframe days with punchcards. Punchcards were all text.



String s = "";

int count = -1;

while ( src.hasNext() ) {

s = src.nextLine();

// you have a definition, class Bill, already

String[] flds = s.split("\t" );

bills[ ++count ] = new Bill( flds );

}



class Bill might look like this



public class Bill {

private boolean basic;

private double west;

...

// the constructor

public Bill( String[] flds ) {

basic = flds[0].equalsIgnoreCase("true" ) ? true : false;

west = Double.parseDouble( flds[ 1 ] )





So, archiving Objects has many ways. Learning the text file way is how a student can visualize the process. Lots of scenarios use a text file and often they have the first line of the file with the number of records.
peatenlane
2016-12-11 16:56:44 UTC
Java has java.application.Scanner; that is one way. you go by ability of the incoming records searching for nextInt(), nextDouble(); the different element you could do is examine a line of textual content at a time and then procedure it. shall we make a sized array to snatch the fields with String.chop up( "t" ); hence the tab personality will chop up the line into array members. actually, it is what we did interior the mainframe days with punchcards. Punchcards were all textual content. String s = ""; int count number = -a million; at the same time as ( src.hasNext() ) { s = src.nextLine(); // you've a definition, classification bill, already String[] flds = s.chop up("t" ); expenditures[ ++count number ] = new bill( flds ); } classification bill would look as if this public classification bill { deepest boolean uncomplicated; deepest double west; ... // the constructor public bill( String[] flds ) { uncomplicated = flds[0].equalsIgnoreCase("real" ) ? real : pretend; west = Double.parseDouble( flds[ a million ] ) So, archiving products has many techniques. getting to understand the textual content record way is how a pupil can visualize the approach. quite some eventualities use a textual content record and in many circumstances they have the first line of the record with the type of records.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...