aspose file tools*
The moose likes Beginning Java and the fly likes How to save to an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to save to an object" Watch "How to save to an object" New topic
Author

How to save to an object

Mark Captain
Ranch Hand

Joined: Nov 01, 2011
Posts: 30
Im working on reading a file with several "bus stops" and each one has certain attributes that i need to save into a bus stop object. This is what i have so far and am quite unsure on what to do next.

the file looks like this with the first number being the number of stops that i have to save, edit: i need to save them into an array
40

1
Downtown Bloomington
40.48419 -88.993353

2
Washington and Clinton
40.479735 -88.984475

3
Heritage Manor
40.486935 -88.984875

4
Vernon and Fell
40.5048029 -88.9870295

5
Amtrak
40.5082769 -88.9836591




here is my code so far
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

So....

1) Can you explain, clearly and precisely, what you think that code does, or what each step is intended to do?

2) What specific problem are you having?

It looks like you're actually quite close though.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Is that anything to do with the 69.1703 miles from yesterday?

How do you plan to save the objects? Serialise them into an object file? Write their data into a text file? Put them into a database? As for arrays: start here.
Mark Captain
Ranch Hand

Joined: Nov 01, 2011
Posts: 30
Campbell Ritchie wrote:Is that anything to do with the 69.1703 miles from yesterday?

How do you plan to save the objects? Serialise them into an object file? Write their data into a text file? Put them into a database? As for arrays: start here.


What I have to do is save each bus stop as an object in an array. I think i understand how i need to go about saving them as a single object but to save them as multiple objects in an array, i dont get how to do this and cant find anything that explains it. It is part of the same problem from yesterday but i dont think it affects it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Campbell, beware of beginners who make up their own terminology!

It looks to me that Mark is using the word "save" where a Java programmer would use the word "assign". I'm guessing that Mark is having problems figuring out how to assign a value to a variable, or perhaps to an array entry.
Mark Captain
Ranch Hand

Joined: Nov 01, 2011
Posts: 30
Paul Clapham wrote:Campbell, beware of beginners who make up their own terminology!

It looks to me that Mark is using the word "save" where a Java programmer would use the word "assign". I'm guessing that Mark is having problems figuring out how to assign a value to a variable, or perhaps to an array entry.


I know how to assign a value to a variable. And i know how to assign values inside arrays. But when objects and constructors are thrown into the mix my brain explodes.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mark Captain wrote:
Paul Clapham wrote:Campbell, beware of beginners who make up their own terminology!

It looks to me that Mark is using the word "save" where a Java programmer would use the word "assign". I'm guessing that Mark is having problems figuring out how to assign a value to a variable, or perhaps to an array entry.


I know how to assign a value to a variable. And i know how to assign values inside arrays. But when objects and constructors are thrown into the mix my brain explodes.


Step 1: Define a class called BusStop. That class apparently has fields like id, address, and location, and it looks like a Location is yet another class that needs to be defined.

Step 2: Create an array of BusStops. Do you know how to do that?

Step 3: Create an instance of a BusStop, and set its fields. You can either do this in the constructor (BusStop stop = new BusStop(someId, someAddress, someLocation);) or you can just create a "blank" BustStop object (BusStop stop = new BusStop();) and then set its fields after the fact. (stop.setId(someId); ... etc. ...) As a general rule, the first approach is preferred, so that the object has a valid initial state before it's available for use. That's not always practical, however, and unless your assignment explicitly requires one approach or the other, for now just go with the one that's easiest for you, and then later, go back and see if you can make it work the other way.


Step 4: Put the newly crated BusStop object into your array.

Step 5: Repeat steps 3 and 4 as many times as necessary.

For Step 3, it looks like you're getting the id, address and location of each stop from a file. Don't let that freak you out. It's the same process we've used right here; namely, break the problem down into smaller pieces, attack the pieces individually, and when you have that figured out, put them in their appropriate place in the whole.

Step 3.1: Read an id from the file.
Rest left as an exercise for the OP.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You’ve got some good advice from Jeff V there. It would appear that a location is identical to a GPS object as described in your previous thread.
Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
Also, watch out for the extra blank lines. nextInt() will only read the integer on the first line but will not read the newline character at the end. So you next nextLine() statement will actually read the newline character of the first line in your file. To get around this, just insert an extra inputFile.nextLine() to move past the newline character of the first line. You might have to do something similar while reading the bus-stops as well if your data file actually has blank lines as shown in your example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to save to an object