Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Xenia Reis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Xenia Reis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all comments!
I'm a total beginner in this and I apologize for silly questions.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic