• Sábado 21 de Septiembre de 2024, 16:03

Autor Tema:  Problema Con Código  (Leído 947 veces)

D4nt3

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Problema Con Código
« en: Lunes 4 de Febrero de 2008, 19:28 »
0
Vereis, estoy haciendo un programa que pasa de números arábigos a romanos y viceversa, com la primera parte no hay problema, pero en la segunda, despues de crear el objeto, al introducir un número romano, me da el siguiente error: Error: incompatible types - found java.lang.String but expected java.lang.String[], entonces supongo q esto quiere decir que ha encontrado un string donde debería haber un array, lo que no consigo encontrar donde está el fallo, me sería de gran ayuda si alguien pudiese mirarselo y si ve el fallo me lo dijese, muchas gracias por adelantado.

Aquí os dejo el código de la segunda parte del programa:

public int convertirRA(final String[] romano)
{
String[] letra = romano;
String[] romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int Arabicvalue = 0;
int locationcounter;

for (locationcounter=0; locationcounter <19; locationcounter++)//this loop looks at each location in the array
{

if (letra[locationcounter] == romans[13])
{
Arabicvalue += 1;
}

else if (letra[locationcounter ]== romans[11])
{
if (letra[locationcounter-1] == romans[13])
{
Arabicvalue += 3;
}
else
{
Arabicvalue += 5;
}
}

else if (letra[locationcounter] == romans[9])
{
if (letra[locationcounter-1] == romans[13])
{
Arabicvalue += 8;
}

else if (letra[locationcounter] == romans[11])
{
Arabicvalue += 0;
}

else
{
Arabicvalue += 10;
}
}


else if (letra[locationcounter] == romans[7])
{
if (letra[locationcounter-1] == romans[13])
{
Arabicvalue += 48;
}

else if (letra[locationcounter] == romans[11])
{
Arabicvalue += 40;
}

else if (letra[locationcounter] == romans[9])
{
Arabicvalue += 30;
}

else
{
Arabicvalue += 50;
}
}


else if (letra[locationcounter] == romans[5])
{
if (letra[locationcounter-1] == romans[13])
{
Arabicvalue += 98;
}

else if (letra[locationcounter] == romans[11])
{
Arabicvalue += 90;
}

else if (letra[locationcounter] == romans[9])
{
Arabicvalue += 80;
}

else if (letra[locationcounter] == romans[7])
{
Arabicvalue += 0;
}

else
{
Arabicvalue += 100;
}
}



else if (letra[locationcounter] == romans[3])
{
if (letra[locationcounter-1] == romans[13])
{
Arabicvalue += 498;
}

else if (letra[locationcounter] == romans[11])
{
Arabicvalue += 490;
}

else if (letra[locationcounter] == romans[9])
{
Arabicvalue += 480;
}

else if (letra[locationcounter] == romans[7])
{
Arabicvalue += 400;
}

else if (letra[locationcounter] == romans[5])
{
Arabicvalue += 300;
}

else
{
Arabicvalue += 500;
}
}

else if (letra[locationcounter] == romans[1])
{
if (letra[locationcounter-1] == romans[13])
{
Arabicvalue += 998;
}

else if (letra[locationcounter] == romans[11])
{
Arabicvalue += 990;
}

else if (letra[locationcounter] == romans[9])
{
Arabicvalue += 980;
}

else if (letra[locationcounter] == romans[7])
{
Arabicvalue += 900;
}

else if (letra[locationcounter]== romans[5])
{
Arabicvalue +=800;
}

else if (letra[locationcounter] == romans[3])
{
Arabicvalue += 0;
}

else
{
Arabicvalue += 1000;
}
}
}
return Arabicvalue;


}

manix

  • Miembro MUY activo
  • ***
  • Mensajes: 203
  • Nacionalidad: cr
    • Ver Perfil
    • http://apuntes.delibertad.com
Re: Problema Con Código
« Respuesta #1 en: Lunes 4 de Febrero de 2008, 23:56 »
0
el error que dices no lo encontre, pero si encontre ArrayBoundsException, es decir, quieres accesar a una pocisicion del array mayor al disponible

1) creo que debes cambiar el 19 por un 13

for (locationcounter=0; locationcounter <19; locationcounter++)//this loop looks at each location in the array

2) creo que tambien debes restarle una unidad a todos las pociciones del array, por ejemplo aqui tenemos un 13 entonces lo cambiamos por 12  

if (letra[locationcounter] == romans[13])

3) y para que no tengas ningun error en el casting es

System.out.println(convertirRA(new String[]{"I", "D", "I", "V", "I", "D", "I", "V","I", "D", "I", "V", "D"}));


 :P