I am at a sticking point with an assignment I have been working on. It is a program that creates a Periodic Table of the Elements. To create each element, I need to take a file(a CSV), split it, and set each split part as an attribute of an object called "Element". Splitting each line of the file and reading it in has so far been no problem. However, I have been stuck on how to take each part and add it as the attribute of the Element object. Can anyone please shed some light on this?
Here is my code:
"Periodic Table" class:
public class PeriodicTable {
private String line;
private String[] parts;
private String filename = "file.csv";
public PeriodicTable() throws IOException {
}
public void readValues(String filename) throws IOException{
Scanner sc = new Scanner(new FileReader(filename));
ArrayList<Element> ar1 = new ArrayList<Element>();
while(sc.hasNextLine()){
line = sc.nextLine();
parts = line.split(",");
Element el = new Element();
el.setChemicalName(parts[0]);
el.setAtomNum(parts[1]);
el.setMeltPoint(parts[3]);
el.setBoilPoint(parts[4]);
el.setDensity(parts[5]);
el.setAtomWeight(parts[6]);
// System.out.println(el.getChemicalName());
//System.out.println(el.getAtomWeight());
ar1.add(el);
}
System.out.println(ar1);
}
The "Element" class
public class Element {
private String chemicalName;
private String atomNum;
private String boilPoint;
private String meltPoint;
private String density;
private String atomWeight;
public Element() throws IOException{
}
/**
* @return the chemicalName
*/
public String getChemicalName() {
return chemicalName;
}
/**
* @param chemicalName the chemicalName to set
*/
public void setChemicalName(String chemicalName) {
this.chemicalName = chemicalName;
}
/**
* @return the atomNum
*/
public String getAtomNum() {
return atomNum;
}
/**
* @param atomNum the atomNum to set
*/
public void setAtomNum(String atomNum) {
this.atomNum = atomNum;
}
/**
* @return the boilPoint
*/
public String getBoilPoint() {
return boilPoint;
}
/**
* @param parts2 the boilPoint to set
*/
public void setBoilPoint(String parts2) {
this.boilPoint = parts2;
}
/**
* @return the meltPoint
*/
public String getMeltPoint() {
return meltPoint;
}
/**
* @param parts2 the meltPoint to set
*/
public void setMeltPoint(String parts2) {
this.meltPoint = parts2;
}
/**
* @return the density
*/
public String getDensity() {
return density;
}
/**
* @param density the density to set
*/
public void setDensity(String density) {
this.density = density;
}
/**
* @return the atomWeight
*/
public String getAtomWeight() {
return atomWeight;
}
/**
* @param input the atomWeight to set
*/
public void setAtomWeight(String input) {
this.atomWeight = input;
}
@Override
public String toString(){
return chemicalName;
}
}
Because the String.split() method uses an array to hold each part that is split, I tried to set the values to each attribute using a reference to the index in the parts array that holds the value, for example....
el.setChemicalName(parts[0]));
However, this doesn't seem to be working - when I try to print out the value of that variable, I usually get a null value.
Any help would be greatly appreciated!
Thanks!
Aucun commentaire:
Enregistrer un commentaire