• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with reading from file and storing Strings into objects of a class  RSS feed

 
Tyler Nichol
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to access certain lines from my file via diverArray diver objects. I am trying to store names and scores of the divers
into the objects if that makes sense. I am newer to java, and have trouble understanding arrays and objects. If you have any
input on my code that would be very helpful. I am not looking for direct answers, just some direction for the new guy.

Attached is my Input.java class where my arrays and objects are created, and my Diver class where the names and scores will be stored.

1) My main question is really whether I am declaring my variables and objects correctly for the arrays.

Thank you for your input!






 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the below comments in the form of constructive criticism, which is what I read your post as asking for. Some of the comments are specifically about code errors and others are just idioms or comments about writing 'safer' code.

Tyler Nichol wrote:I need to access certain lines from my file via diverArray diver objects. I am trying to store names and scores of the divers
into the objects if that makes sense. I am newer to java, and have trouble understanding arrays and objects. If you have any
input on my code that would be very helpful. I am not looking for direct answers, just some direction for the new guy.

Attached is my Input.java class where my arrays and objects are created, and my Diver class where the names and scores will be stored.

1) My main question is really whether I am declaring my variables and objects correctly for the arrays.

So tell us this, what happens when you compile the code? What happens when you try to run it?

Tyler Nichol wrote:Thank you for your input!


This is my first point of concern. Why do you have this variable? Is it used?

Another point that I like to make is this: When possible, use final variables, its just safer. Do you expect the firstName and lastName variables to change after the constructor? If not, then make them final. You probably can't do the same thing with scores, unless you know that the length of the scores array should never change.

One final note: the finalScore seems like a calculated value. It might be that you don't need to have a variable to hold that value. If you do, then make sure you keep it up to date by re-calculating it whenever scores get set.

Tyler Nichol wrote:

Here you set only the first value in the scores array. There are a couple of problems with this: first, you never initialized the this.scores array, so it is null, and you will probably get an exception. Secondly, arrays in Java are not like arrays in C/C++ when you assign the 'head' of the this.scores to a value, the rest of the values don't come along - arrays aren't just a pointer to the start of a sequence. You have to do one of the following:
* Assign the this.scores refrence to the same array referred to by the local variable scores. For example, this.scores = scores; This is probably the closest thing to what you have in your code.
* Assign each index in the this.scores array the value of the same index in the local variable scored. Note you first have to initialize the this.scores variable to a double array of the correct size, and that the assignment is probably best to be done in a for loop. Also note that, although more code and slower than the previous option, this option is the safest because no code outside the class can modify the Diver's score unexpectedly.

Tyler Nichol wrote:

Before, when I said that I like to make variables final, that would mean that I generally don't like the setNNN() methods, unless you think the values can and should change during the course of the Object's life. Does it make sense that the Diver's name change? If it does, then keep these set methods and don't make the variables final. If not then get rid of the setNNN methods and make the variables final.

Also, what is the point of passing in the Diver Object?

Tyler Nichol wrote:


This returns just the first score in the scores array. If this is what you want, then ok, if not then you need to return the full array, and make the return type a double array, not just a double. Also I will interject here more about code safety: I often will make a duplicate array, fill it with the values of this.scores and return the duplicate. Again, this is more work and takes longer, but it protects the inter data from manipulation outside the class.

Tyler Nichol wrote:

The comments I made in the constructor about the scores array and assignment apply here as well.
Tyler Nichol wrote:

There is a chance that the finalScore has never been set, and so this method could return 0.0 even if scores have been assigned. I would consider this an 'inconsistent state.' If the final score is dependent and calculated from the scores, then it should be calculated either in the get method, or it should be calculated when you set the scores.
Tyler Nichol wrote:

I would be a little worried about this method. If the finalScore is calculated from the scores, then this method should handle the calculation itself, and do so in a way that prevents an inconsistent state. Relying on an external source to do calculations for the data this class holds partially circumvents the purpose of using classes. If at all possible, get rid of this set method, and make the calculation in-class, and at a point where there can be no inconsistent state.

This assumes the finalScore is calculated from scores, though. If it is not, or if it relies on other data that this class does not have access to then these points are moot.
Tyler Nichol wrote:
 
Tyler Nichol
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your time, and input for this post.

1) When I run the program, all I have right now is the output for the File that I have created.
Using this code, followed by the output.

public Diver[] readDiver(){
//Create the diver objects and fill with info
Diver diver1 = new Diver();
Diver diver2 = new Diver();
Diver diver3 = new Diver();
Diver diver4 = new Diver();
Diver diver5 = new Diver();
Diver diver6 = new Diver();
Diver diver7 = new Diver();
Diver diver8 = new Diver();
Diver diver9 = new Diver();
Diver diver10= new Diver();

// //Create Diver array
Diver[] diverArray = new Diver[NUM_DIVERS];

// //Fill the array with the diver objects

diverArray[0] = diver1;
diverArray[1] = diver2;
diverArray[2] = diver3;
diverArray[3] = diver4;
diverArray[4] = diver5;
diverArray[5] = diver6;
diverArray[6] = diver7;
diverArray[7] = diver8;
diverArray[8] = diver9;
diverArray[9] = diver10;


for(int i = 0; i<diverArray.length; i++){

diverArray[i]=new Diver();

diverArray[i].setDiverFirstName(diveFile.nextLine());

System.out.println(diverArray[i].getDiverFirstName());
System.out.println(" ");


}


return diverArray;



THIS IS THE OUTPUT/FILE I HAVE READ FROM THE USE OF ARRAYS

Tyler Nicholson 9.5 9.2 8.2 8.1 9.0 9.4

Alyse Lewis 9.4 9.8 9.7 9.6 8.9 9.1

Nick Sivek 8.4 8.3 9.4 8.8 9.1 9.0

Adam Bourjaily 8.5 9.0 9.0 8.4 8.9 8.7

Mike Felps 10.0 9.9 9.7 9.6 9.3 8.9

Joe Shmo 7.4 8.3 8.2 8.1 9.0 8.7

John Smith 8.8 8.9 9.0 7.9 8.0 9.3

James Brown 9.0 9.1 9.3 9.2 9.4 8.8

Jane Jill 8.9 7.7 8.8 9.0 9.1 9.2

Jack Nicholson 8.7 6.8 6.9 8.3 8.4 8.9


My Main concern. . .

2) I want to store each String line into an object, and I am not sure how to accomplish that.
I have read a lot of forums, java book, and researched online. I just can't seem to grasp
the concept of arrays and when and where I am able to call them.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is a very simple answer to when you can use arrays and where. Any time and wnywhere. Have a look at the little description in the Java Tutorials. You can put anything into an array as long as there isn’t “<ANYTHING>” in the class’ name.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would do well to create a Diver class which incorporates the marks as a field. You can try a double[] type of field; that is probably the simplest.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!