Reputation: 4115
I'm trying to convert a method that I have written in C# to Java for Android.
The following coding is what I'm trying to convert from C# to Java:
public Map()
{
string line;
int maxRowLevels, maxColLevels;
StreamReader file;
file = File.OpenText("map.txt");
line = file.ReadLine();
line = file.ReadLine();
maxRowLevels = System.Convert.ToInt32(line) - 1;
line = file.ReadLine();
line = file.ReadLine();
maxColLevels = System.Convert.ToInt32(line) - 1;
line = file.ReadLine();
map = new char[maxRowLevels+1,maxColLevels+1,screenRows,screenCols];
for (int rowCurrentLevel = 0; rowCurrentLevel<=maxRowLevels; rowCurrentLevel++)
{
for (int currentRow = 0; currentRow<screenRows; currentRow++)
{
line = file.ReadLine();
for (int colCurrentLevel = 0; colCurrentLevel<=maxColLevels; colCurrentLevel++)
{
for (int currentCol = 0; currentCol<screenCols; currentCol++)
{
map[rowCurrentLevel, colCurrentLevel,currentRow, currentCol] = line[colCurrentLevel*screenCols + currentCol];
}
}
}
}
file.Close();
}
My attempt to conversion is the following code:
public Map(Context context)
{
String line;
int maxRowLevels, maxColLevels;
int maxRowLevels, maxColLevels;
InputStream inputFile;
BufferedReader file;
inputFile = context.getAssets().open("map.txt");
file = new BufferedReader(new InputStreamReader(inputFile, "UTF-8"));
line = file.readLine();
line = file.readLine();
maxRowLevels = Integer.parseInt(line)-1;
line = file.readLine();
line = file.readLine();
maxColLevels = Integer.parseInt(line)-1;
line = file.readLine();
mapa = new char[maxRowLevels+1][maxColLevels+1][screenRows][screenCols];
for (int rowCurrentLevel = 0; rowCurrentLevel<=maxRowLevels; rowCurrentLevel++)
{
for (int currentRow = 0; currentRow<screenRows; currentRow++)
{
line = file.readLine();
for (int colCurrentLevel = 0; colCurrentLevel<=maxColLevels; colCurrentLevel++)
{
for (int currentCol = 0; currentCol<screenCols; currentCol++)
{
mapa[rowCurrentLevel][colCurrentLevel][currentRow][currentCol] = line[colCurrentLevel*screenCols + currentCol];
}
}
}
}
inputFile.close();
file.close();
}
It seems everything is correct except for this line that drops this error: "the type of the expression Must Be an array type string But It resolved to string"
mapa[rowCurrentLevel][colCurrentLevel][currentRow][currentCol] = line[colCurrentLevel*screenCols + currentCol]
;
Can anyone tell me how to fix it? I think that it will be silly, forgive me but I am a begginer in Java.
Thanks in advance.
Upvotes: 2
Views: 742
Reputation: 8679
I think the problem is that in java the String class doesn't support the [] operator to access a specific char, so you need to call line.charAt(colCurrentLevel*screenCols + currentCol)
instead of line[colCurrentLevel*screenCols + currentCol]
Upvotes: 2
Reputation: 2856
You can't acess to character in stirng with []
Use line.charAt(colCurrentLevel*screenCols + currentCol);
instead
Upvotes: 1
Reputation: 53516
You don't say where it is but I am betting it is here...
line[colCurrentLevel*screenCols + currentCol]
Which should be using String's charAt() since lines are not treated as char arrays in Java...
line.charAt(colCurrentLevel*screenCols + currentCol)
Upvotes: 3