2

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.

Trisped
  • 5,705
  • 2
  • 45
  • 58
karse23
  • 4,055
  • 6
  • 29
  • 33
  • `c#` tag added, I don't know if it should be `c++`, but it surely isn't `c` – pmg Mar 13 '11 at 19:44
  • there is a typo in the 2nd line "I'm trying to convert a method that I have written i C to java for Android." , should be in C# instead of i c – Ahmed Kotb Mar 13 '11 at 19:47
  • See here: http://stackoverflow.com/questions/78811/is-there-an-effective-tool-to-convert-c-sharp-code-to-java-code – Anderson Green Nov 06 '12 at 17:59

3 Answers3

3

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)
Andrew White
  • 52,720
  • 19
  • 113
  • 137
2

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]

CarlosZ
  • 8,281
  • 1
  • 21
  • 16
1

You can't acess to character in stirng with [] Use line.charAt(colCurrentLevel*screenCols + currentCol); instead

Max Komarychev
  • 2,836
  • 1
  • 21
  • 32