Trying to solve - String index out of range

I'm getting this error:

Carregando os mapas: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String 
index out of range: 4800
        at java.lang.String.substring(Unknown Source)
        at estaticos.Criptografador.descompilarMapaData(Criptografador.java:147)
        at variables.Mapa.<init>(Mapa.java:1306)
        at estaticos.GestorSQL.CARREGAR_MAPAS(GestorSQL.java:696)
        at estaticos.MundoDofus.criarServer(MundoDofus.java:1398)
        at estaticos.VrauEMU.main(VrauEMU.java:130)

I'm trying to solve but i can't.

My code: (the original emulator is spanish, so celula = celda before)

public static Map<Integer, Celula> descompilarMapaData(Mapa mapa, String dData) {
        Map<Integer, Celula> celulas = new TreeMap<Integer, Celula>();
        for (int f = 0; f < dData.length(); f += 10) {
            String CellData = dData.substring(f, f + 10);
            List<Byte> celulaInfo = new ArrayList<Byte>();
            for (int i = 0; i < CellData.length(); i++)
                celulaInfo.add((byte) getNumeroPorValorHash(CellData.charAt(i)));
            int caminhavel = (celulaInfo.get(2) & 56) >> 3;// 0 = nao, 1 = meio, 4 = sim
            boolean linhaDeVisao = (celulaInfo.get(0) & 1) != 0;
            int layerObject2 = ( (celulaInfo.get(0) & 2) << 12) + ( (celulaInfo.get(7) & 1) << 12) + (celulaInfo.get(8) << 6)
                    + celulaInfo.get(9);
            boolean layerObjeto2Interac = ( (celulaInfo.get(7) & 2) >> 1) != 0;
            int objeto = (layerObjeto2Interac ? layerObject2 : -1);

            celulas.put(f / 10, new Celula(mapa, f / 10, caminhavel != 0, linhaDeVisao, objeto));
        }
        return celulas;

Upvotes: 0

Views: 69

Answers (3)

Bablu Singh
Bablu Singh

Reputation: 94

public static Map<Integer, Celula> descompilarMapaData(Mapa mapa, String dData) {
        Map<Integer, Celula> celulas = new TreeMap<Integer, Celula>();
        //changes are here
          for (int f = 0; f <= dData.length() - 10; f += 10) {
            String CellData = dData.substring(f, f + 10);
            List<Byte> celulaInfo = new ArrayList<Byte>();
            for (int i = 0; i < CellData.length(); i++)
                celulaInfo.add((byte) getNumeroPorValorHash(CellData.charAt(i)));
            int caminhavel = (celulaInfo.get(2) & 56) >> 3;// 0 = nao, 1 = meio, 4 = sim
            boolean linhaDeVisao = (celulaInfo.get(0) & 1) != 0;
            int layerObject2 = ((celulaInfo.get(0) & 2) << 12) + ((celulaInfo.get(7) & 1) << 12) + (celulaInfo.get(8) << 6)
                    + celulaInfo.get(9);
            boolean layerObjeto2Interac = ((celulaInfo.get(7) & 2) >> 1) != 0;
            int objeto = (layerObjeto2Interac ? layerObject2 : -1);

            celulas.put(f / 10, new Celula(mapa, f / 10, caminhavel != 0, linhaDeVisao, objeto));
        }
        return celulas;
    }

Try this I think it would work for you.

Upvotes: 0

Thank you for the answer :)

I put the code:

 for (int f = 0; f < dData.length(); f += 10) {
            int upperBound = min(f + 10, dData.length());
            String CellData = dData.substring(f, upperBound));

And i got this now:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
        at java.util.ArrayList.rangeCheck(Unknown Source)
        at java.util.ArrayList.get(Unknown Source)
        at estaticos.Criptografador.descompilarMapaData(Criptografador.java:152)
        at variables.Mapa.<init>(Mapa.java:1306)
        at estaticos.GestorSQL.CARREGAR_MAPAS(GestorSQL.java:696)
        at estaticos.MundoDofus.criarServer(MundoDofus.java:1398)
        at estaticos.VrauEMU.main(VrauEMU.java:130)

I think it's almost there

Upvotes: 0

junvar
junvar

Reputation: 11574

The issue is with the iteration bounds:

 for (int f = 0; f < dData.length(); f += 10) {
            String CellData = dData.substring(f, f + 10);

Unless dData.length() happens to be divisible by 10, this will iterate out of bounds. To illustrate, the below snippet will try to read 0-10 and 10-20 even though the imaginary array bounds are only 0-15.

for (let i = 0; i < 25; i += 10)
  console.log(i, i + 10);

Perhaps simply capping the upper bound to substring.length() is sufficient:

 for (int f = 0; f < dData.length(); f += 10) {
            int upperBound = min(f + 10, dData.length());
            String CellData = dData.substring(f, upperBound));

Upvotes: 1

Related Questions