Here is my program question : In this problem you are to write a program to explore the given array for a treasure. The values in the array are clues. Each cell contains an integer between 11 and 55; for each value the ten's digit represents the row number and the unit's digit represents the column number of the cell containing the next clue. Starting in the upper left corner (at 1,1), use the clues to guide your search of the array. (The first three clues are 11, 34, 42). i.e. Cell (1,1) contains 34, which means go to cell 3, 4 Cell(3,4) contains 42 which means go to cell 4,2….. [B]The treasure is a cell whose value is the same as its coordinates.[/B] Your program must first read in the treasure map data into a 5 by 5 array. Your program should output the cells it visits during its search, and a message indicating where you found the treasure. You will need to use arrays, methods, and objects.
import java.io.*;
public class TreasureHunt {
public static void main(String[] args) {
String[] lines = new String[0];
String path = "map01.csv";
BufferedReader br = null;
try {
File file = new File(path);
br = new BufferedReader(
new InputStreamReader(
new FileInputStream(file)));
String line;
while( (line = br.readLine()) != null ) {
lines = add(line, lines);
}
br.close();
} catch(IOException e) {
System.out.println("read error: " + e.getMessage());
}
print(lines);
}
private static String[] add(String s, String[] array) {
int len = array.length;
String[] temp = new String[len+1];
System.arraycopy(array, 0, temp, 0, len);
temp[len] = s;
return temp;
}
private static void print(String[] data) {
for(int i = 0; i < data.length; i++)
System.out.println(data[i]);
}
}
To go to the next cell, I know I will have to use row = num/10 and column = num%10; If I do this: clue = array[row=num/10][row%10], will it work? Is this the right way to do it? Please help!


Sign In
Create Account


Back to top









