Reputation: 96
I have an assignment to create a "map" which is really an array that contains rooms. In the rooms there are artifacts that you can look at or examine and put into your inventory or "backpack". You are also able to save and restore your placement on the map, along with the artifact locations and your inventory. The save function saves it to a text file with the location, artifacts, and inventory. The problem that I am running into is trying to get the artifact data from the text file to restore on the program. I have no problem loading the location, but can not load any artifacts. While running the debugger it skips over the line. Here is the piece of code I am trying to work.
File fileRestore = new File("C:\\Users\\mike\\Desktop\\GCPU.txt");
try
{
FileReader reader = new FileReader(fileRestore);
BufferedReader buffer = new BufferedReader(reader);
String line = buffer.readLine();
while (line !=null)
{
String[] contents = line.split(",");
String key = contents[0];
if (key.equals("StartLocation"))
{
row = Integer.parseInt(contents[1]);
col = Integer.parseInt(contents[2]);
}
key = contents [1];
if (key.equals("Artifact"))
{
String name = contents [1];
row = Integer.parseInt(contents [2]);
col = Integer.parseInt(contents [3]);
if (name.equals("vending"))
map.rooms[row][col].contents = map.vending;
if (name.equals("beaker"))
map.rooms[row][col].contents = map.beaker;
if (name.equals("gameboy"))
map.rooms[row][col].contents = map.gameboy;
if (name.equals("paper"))
map.rooms[row][col].contents = map.paper;
if (name.equals("trees"))
map.rooms[row][col].contents = map.trees;
if (name.equals("desk"))
map.rooms[row][col].contents = map.desk;
if (name.equals("brewer"))
map.rooms[row][col].contents = map.brewer;
if (name.equals("statue"))
map.rooms[row][col].contents = map.statue;
}
Here is the text file that I am reading from:
StartLocation,0,2
Artifact,Eerie statue,2,0
Artifact,A small redwood sprout,3,0
Artifact,Gameboy Color,0,1
Artifact,Lapdesk,1,1
Artifact,A piece of paper,2,1
Artifact,Industrial coffee maker,1,3
So to reiterate my question, how am I able to read the Artifact line from the text along with the StartLocation line?
Thank you for taking the time to read this.
Upvotes: 0
Views: 114
Reputation: 36304
- String line = buffer.readLine(); will skip over the first line.
2.
if (key.equals("StartLocation"))
{ row = Integer.parseInt(contents[1]);
col = Integer.parseInt(contents[2]);
}
will fail because key = " StartLocation". there is a space in your
text file. use line=line.trim() to remove unnecessary characters.
3. Your program will read only 1 line.
Upvotes: 0
Reputation: 7057
String line = buffer.readLine();
should be inside the while loop. It is being executed only once.
Although make sure to modify initial value of line
and the loop condition such that the loop starts .
Good luck.
Upvotes: 0
Reputation: 52185
You are only reading once from the file. Replacing these two lines:
String line = buffer.readLine();
while (line !=null)
...
With something like so:
String line = "";
while((line = buffer.readLine()) != null)
...
Should allow you to read the next line each time the while
loop iterates.
Alternatively, you could also have a line = buffer.readLine()
just before your closing brace for the while
loop. This approach is also valid, however, judging by the tutorials I went through, simply less popular.
Upvotes: 2