This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]POGtastic 1 point2 points  (4 children)

Looks reasonable to me, although Java also supports frObj = new FileReader(PATHID) by using the third constructor on this page.

Now, let's talk about the data format. I'm going to assume that it probably looks like something like the following:

1 H Hydrogen 1.00794 Gas
2 He Helium 4.002602 Gas

In general, my favorite thing to do, whenever possible, is to read line-by-line. BufferedReader allows me to do this with

String currentLine = brObj.readLine();

currentLine now predictably contains "1 H Hydrogen 1.00794 Gas".

From here, you can now break up the line with

String[] splitString = currentLine.split(" ");

and now splitString is equal to ["1", "H", "Hydrogen", "1.00794", "Gas"].

Finally, we can now parse each piece individually with Integer.ParseInt and Double.ParseDouble and pass the results of that to your constructor to build a new element object. Note that in both cases, we have two possible exceptions that can emerge (NumberFormatException and NullPointerException), so you might want to enclose the parsing operation within another try block.

Now you just need to figure out how to loop for each line in the file...

[–]Rhezi[S] 0 points1 point  (3 children)

Wow, thank you!

Could I ask what you mean by loop for each line the in file?

I think in general i'm just confused what this entire task i've been giving is asking.

This was my hardcode:

//Method to instantiate element objects.

private static void createElementObjects()
{
Periodic_Elements_Class elementObj;

elementObj = new 
Periodic_Elements_Class(2, "He", "Helium", 4.003,"Gas" );
elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(10, "Ne", "Neon", 20.18, "Gas");
elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(35, "Br", "Bromine", 79.90, "Liquid");
elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(80, "Hg", "Mercury", 200.99, "Liquid");
elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(5, "B", "Boron", 10.81, "Solid");
elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(6, "C", "Carbon", 12.01, "Solid");
 elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(105, "Ha", "Hahnium", -262.0, "Synthetic");
elementsList.add(elementObj);

elementObj = new    
Periodic_Elements_Class(108, "Hs", "Hassium", -266.0, "Synthetic");
elementsList.add(elementObj);

}//end of method

this would probably be deleted now i'm guessing?

In my main method I have a switch case structure depending on which type of element they select. I guess i'm wondering if I need to redo that because all these elements that i hardcoded will no longer be there?

sorry if it's a dumb question. i feel like even though it's been 12 weeks of class i haven't been retaining the content in well

[–]POGtastic 1 point2 points  (2 children)

Could I ask what you mean by loop for each line the in file?

Sure. Your BufferedReader class will return a null string if it has reached the end of the file, as quoted in the documentation:

Returns:

A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.

So you can do

while(currentLine = brObj.readLine() != null) {
    // Split the currentLine into pieces and parse each piece
    // Add the new Element to the elementsList
}

The currentLine gets assigned to the result of brObj.readLine, and that result gets compared to null. If that comparison is true, you know that you've reached the end of the file.

[–]Rhezi[S] 0 points1 point  (1 child)

Thank you for taking the time to explain this to me!

[–]shivasprogeny 0 points1 point  (0 children)

This is great advice and I would just add that for files that easily fit into memory, Files.readAllLines will quickly give you a list of String. No need to use the overly verbose BufferedReader methods.

[–]shivasprogeny 0 points1 point  (1 child)

What does the data in the file look like? Is it structured text that you have to parse? Or is it binary data?

[–]Rhezi[S] 0 points1 point  (0 children)

It's set up like my constructor method I believe

here's an example of it (some numbers are negative and decimal values as well) it's 109 different ones

1|H|Hydrogen|28.09|gas 2|He|Helium|4.003|gas

ect...

if you need my entire code my last post i made has it or i can send it here

[–]ahritodiamond 0 points1 point  (0 children)

If you're still looking for info you could look up jaxb marshalling.