File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes reading values from txt file via loop and storing them into class instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading values from txt file via loop and storing them into class instances" Watch "reading values from txt file via loop and storing them into class instances" New topic
Author

reading values from txt file via loop and storing them into class instances

Xenia Reis
Greenhorn

Joined: Nov 28, 2011
Posts: 3
Hello!

I'm trying to code a sea battle game for my java assignment. I'm suppose to read attributes for each ship from a txt file and then initiate new ships. The text file has this:

4 ---> total number of ships in game
Boat 1;AircraftCarrier;C2;C3;C4;C5
Boat 2;Submarine;E6;F6;G6
Boat 3;AircraftCarrier;E2;F2;G2;H2
Boat 4;Submarine;A6;A7;A8

I need a hint on how to store the values from the txt file which i read via while loop into new objects of a class Ship.
Here is a piece of code I'm using now:


But it seem not to work! Any good advice is appreciated!

X.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You mean you can't tell whether it works or not? If that's what you mean, then you should do something -- like trying to compile or run the code -- and see what kind of error notifications you see. Take note of those things because they will tell you what problems are present.

Or did you perhaps mean you already did that, and you found some problems? If so then read this FAQ entry: TellTheDetails.
Xenia Reis
Greenhorn

Joined: Nov 28, 2011
Posts: 3
It doesn't compile. I get Caught ArrayIndexOutOfBoundsException: 0. I'm not quite sure whether I do it right. I would like to know how to store details from the loop into new instances.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
xenia sh wrote:It doesn't compile. I get Caught ArrayIndexOutOfBoundsException: 0. . . .
That means it does compile.

I can see several places where you might get such an Exception; please provide more details.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
And welcome to the Ranch
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

You do this:


When the Ships[] array is made, the totalNumbeOfShips value has not been assigned to yet, so it uses the the default value for an int, which is 0. Later you do this:

But Ship is still a size 0 array, it can't even hold one Ships in it. You need to make Ship large enough to hold all your Objects. If you don't know how many that is when you declare the variable then you need to create the array later, after you do know how many Objects it will hold.

Also, I think the code is a bit confused. You have the Ship variable, of type Ships[], then you attempt to assign a Ship Object to one of the indexes. A Ship (singular) is not the same as a Ships (plural). Make sure you have no typos...


Steve
Xenia Reis
Greenhorn

Joined: Nov 28, 2011
Posts: 3
Thanks for all comments!
I'm a total beginner in this and I apologize for silly questions.

 
 
subject: reading values from txt file via loop and storing them into class instances
 
Similar Threads
JAVA BATTLESHIP HELP ! PLEASE
Read in file
Need to speed this algorithm up
variable problem
Class assignment results not as expected