Normas del foro

Curso Hacker
Bienvenido(a), Visitante. Favor de ingresar o registrarse.
¿Perdiste tu email de activación? - Diciembre 05, 2008, 12:15:58
Inicio Ayuda Ingresar Registrarse
Visita: Articulos - Juegos Gratis - Da Foros

Comunidad Underground Hispana  |  Programacion  |  Programacion para webmasters  |  Java - Java Script (Moderador: -rOnNy-)  |  Tema: Duda con arreglo de numeros romanos 0 Usuarios y 1 Visitante están viendo este tema. « anterior próximo »
Páginas: [1] Ir Abajo Imprimir
Autor Tema: Duda con arreglo de numeros romanos  (Leído 357 veces)
__Horus__
Recien Llegado
*
Desconectado Desconectado

Mensajes: 2


Ver Perfil Email
« en: Marzo 13, 2008, 11:00:05 »

juegos gratis
Hola!

Nos pidieron en la escuela un programa que pasa un numero que uno le ingrese por el teclado al programa y lo pase a numero romano  menor que 3999 pero con arreglos y con to.charArray y partir de ellos para pasarlos de unidades, decenas, centenas y milesimas dependiendo su pocision el codigo en si lo tengo compilado en eclipse y no me marca errores pero al ejecutarlo me muestra la exception

345<---- este le ingrese
java.lang.ArrayIndexOutOfBoundsException: 3
   at Romano.main(Romano.java:198)
Exception in thread "main"

Segun se  la exception ArrayIndexOutOfBoundsException  aparece cuando los subindices del arreglo estan fuera de los limites establecidos por la matriz pero segun yo lo ando pasando el String a mi arreglo romano de char y partiendolo en pocision a int sin embargo me genera esa exception

Espero me puedan ayudar

Aqui esta el codigo:
Esta algo larguito

import java.io.DataInputStream;
import java.io.IOException;


public class Romano {

   
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      String romanos=leeDato();
      char[] romano=romanos.toCharArray();
     
      if(romano.length==4){
         int r=romano[0];
         
         switch(r){
         case 1:
         System.out.print("M");
         
         break;
         case 2:
         System.out.print("MM");
         
         break;
         case 3:
         System.out.print("MMM");
         
         break;
         }
         int e=romano[1];
         
         switch(e){
         case 1:
            System.out.print("C");
            break;
            case 2:
            System.out.print("CC");
            break;
            case 3:
            System.out.print("CCC");
            break;
            case 4:
            System.out.print("CD");
            break;
            case 5:
            System.out.print("D");
            break;
            case 6:
            System.out.print("DC");
            break;
            case 7:
            System.out.print("DCC" );
            break;
            case 8:
            System.out.print("DCCC");
            break;
            case 9:
            System.out.print("DM");
            break;
            }
         
         int w=romano[2];
         
         
         switch(w){
         case 1:
         System.out.print("X");
         break;
         case 2:
         System.out.print("XX");
         break;
         case 3:
         System.out.print("XXX");
         break;
         case 4:
         System.out.print("XL");
         break;
         case 5:
         System.out.print("L");
         break;
         case 6:
         System.out.print("LX");
         break;
         case 7:
         System.out.print("LXX" );
         break;
         case 8:
         System.out.print("LXXX");
         break;
         case 9:
         System.out.print("XC");
         break;
         }
         int q=romano[3];
     
         
         
         switch(q){
         case 1:
         System.out.print("I");
         break;
         case 2:
         System.out.print("II");
         break;
         case 3:
         System.out.print("III");
         break;
         case 4:
         System.out.print("IV");
         break;
         case 5:
         System.out.print("V");
         break;
         case 6:
         System.out.print("VI");
         break;
         case 7:
         System.out.print("VII" );
         break;
         case 8:
         System.out.print("VIII");
         break;
         case 9:
         System.out.print("IX");
         break;
         }
         }
     
     
if(romano.length==3){
   
   int e=romano[1];
   
   switch(e){
   case 1:
      System.out.print("C");
      break;
      case 2:
      System.out.print("CC");
      break;
      case 3:
      System.out.print("CCC");
      break;
      case 4:
      System.out.print("CD");
      break;
      case 5:
      System.out.print("D");
      break;
      case 6:
      System.out.print("DC");
      break;
      case 7:
      System.out.print("DCC" );
      break;
      case 8:
      System.out.print("DCCC");
      break;
      case 9:
      System.out.print("DM");
      break;
      }
   
   int w=romano[2];

   
   switch(w){
   case 1:
   System.out.print("X");
   break;
   case 2:
   System.out.print("XX");
   break;
   case 3:
   System.out.print("XXX");
   break;
   case 4:
   System.out.print("XL");
   break;
   case 5:
   System.out.print("L");
   break;
   case 6:
   System.out.print("LX");
   break;
   case 7:
   System.out.print("LXX" );
   break;
   case 8:
   System.out.print("LXXX");
   break;
   case 9:
   System.out.print("XC");
   break;
   }
   int q=romano[3];

   
   
   switch(q){
   case 1:
   System.out.print("I");
   break;
   case 2:
   System.out.print("II");
   break;
   case 3:
   System.out.print("III");
   break;
   case 4:
   System.out.print("IV");
   break;
   case 5:
   System.out.print("V");
   break;
   case 6:
   System.out.print("VI");
   break;
   case 7:
   System.out.print("VII" );
   break;
   case 8:
   System.out.print("VIII");
   break;
   case 9:
   System.out.print("IX");
   break;
   }
      }
if(romano.length==2){
   int w=romano[2];
   
   
   switch(w){
   case 1:
   System.out.print("X");
   break;
   case 2:
   System.out.print("XX");
   break;
   case 3:
   System.out.print("XXX");
   break;
   case 4:
   System.out.print("XL");
   break;
   case 5:
   System.out.print("L");
   break;
   case 6:
   System.out.print("LX");
   break;
   case 7:
   System.out.print("LXX" );
   break;
   case 8:
   System.out.print("LXXX");
   break;
   case 9:
   System.out.print("XC");
   break;
   }
   int q=romano[3];
     
   switch(q){
   case 1:
   System.out.print("I");
   break;
   case 2:
   System.out.print("II");
   break;
   case 3:
   System.out.print("III");
   break;
   case 4:
   System.out.print("IV");
   break;
   case 5:
   System.out.print("V");
   break;
   case 6:
   System.out.print("VI");
   break;
   case 7:
   System.out.print("VII" );
   break;
   case 8:
   System.out.print("VIII");
   break;
   case 9:
   System.out.print("IX");
   break;
   }
   
}
if(romano.length==1){
   int q=romano[3];
   
   switch(q){
   case 1:
   System.out.print("I");
   break;
   case 2:
   System.out.print("II");
   break;
   case 3:
   System.out.print("III");
   break;
   case 4:
   System.out.print("IV");
   break;
   case 5:
   System.out.print("V");
   break;
   case 6:
   System.out.print("VI");
   break;
   case 7:
   System.out.print("VII" );
   break;
   case 8:
   System.out.print("VIII");
   break;
   case 9:
   System.out.print("IX");
   break;
   }
   
}
     
     
   }
   public static String leeDato() {
      String dato = "";
      try {
         DataInputStream intro = new DataInputStream(System.in);
         dato = intro.readLine();
      } catch (IOException e) {
         System.out.println("leeDato-- Error de Entrada/Salida : " + e);
      }
      return dato;
   }
}

En línea
[p3ll3]
Colaborador
*****
Desconectado Desconectado

Mensajes: 535


... Open your eyes , open your mind ...


Ver Perfil WWW
« Respuesta #1 en: Marzo 15, 2008, 01:59:58 »

Men te sale ese error porque la logica del programa no esta muy bien que digamos , estas haciendo una comprobacion inexistente, buscas un elemento en la posicion 3 del arreglo cuando en realidad solo tienes 2 posiciones en el arreglo (en el caso del ejemplo que pusiste), trata de pensar un poco mas y despues me cuentas si lo pudiste hacer, yo ya lo corregi , te voy a dar un tiempo y si no puedes del todo te pongo el code corregido.


Saludos........................... Cool
En línea


Necesitas ser usuario para ver los enlaces Crear Usuario  Hacer Sesion


Necesitas ser usuario para ver los enlaces Crear Usuario  Hacer Sesion

[p3ll3]
Colaborador
*****
Desconectado Desconectado

Mensajes: 535


... Open your eyes , open your mind ...


Ver Perfil WWW
« Respuesta #2 en: Marzo 15, 2008, 02:56:59 »

Bueno lo posteo ya que veo que habia sido posteado hace dos dias y seguro ya no le sirve , aqui esta el programa , se puede hacer un 10000 % mejor  Grin Grin , pero lo hice con la logica parecida a la de _Horus_ para que no se perdiera si lo llega a ver.

Cualquier duda pregunten. Wink

Código:
import java.io.*;

// cambiar numeros digitos decimales a signos numeros
//romanos.
public class NumerosRomanos {

    public static BufferedReader entrada=new BufferedReader(new InputStreamReader(System.in));
   
    public static void main(String[]args)throws IOException{
       
       
        Entrada();
       
       
    }
    public static void Entrada()throws IOException{
       
        System.out.print("Digite numero a convertir: ");
        String ent = entrada.readLine();
        int valor = Integer.parseInt(ent);
        if( (valor >= 4000) || (valor<= 0) ){
            System.out.println("Valor no esta dentro del rango permitido,intente de nuevo");
            Entrada();
        }else{
        char[] romano=ent.toCharArray();
        int tam = romano.length;
        Transformar(romano,tam);
        }
    }
    public static void Transformar(char[] romano,int tam){
     
             
      if(tam == 4) {
     
         switch(romano[0]){
          case '1':
          System.out.print("M");
          break;
          case '2':
          System.out.print("MM");
          break;
          case '3':
          System.out.print("MMM");
          break;
         }
         switch(romano[1]){
        case '1':
        System.out.print("C");
        break;
        case '2':
        System.out.print("CC");
        break;
        case '3':
        System.out.print("CCC");
        break;
        case '4':
        System.out.print("CD");
        break;
        case '5':
        System.out.print("D");
        break;
        case '6':
        System.out.print("DC");
        break;
        case '7':
        System.out.print("DCC" );
        break;
        case '8':
        System.out.print("DCCC");
        break;
        case '9':
        System.out.print("DM");
        break;
      }
      switch(romano[2]){
       case '1':
       System.out.print("X");
       break;
       case '2':
       System.out.print("XX");
       break;
       case '3':
       System.out.print("XXX");
       break;
       case '4':
       System.out.print("XL");
       break;
       case '5':
       System.out.print("L");
       break;
       case '6':
       System.out.print("LX");
       break;
       case '7':
       System.out.print("LXX" );
       break;
       case '8':
       System.out.print("LXXX");
       break;
       case '9':
       System.out.print("XC");
       break;
     }
     switch(romano[3]){
           case '1':
           System.out.print("I");
           break;
           case '2':
           System.out.print("II");
           break;
           case '3':
           System.out.print("III");
           break;
           case '4':
           System.out.print("IV");
           break;
           case '5':
           System.out.print("V");
           break;
           case '6':
           System.out.print("VI");
           break;
           case '7':
           System.out.print("VII" );
           break;
           case '8':
           System.out.print("VIII");
           break;
           case '9':
           System.out.print("IX");
           break;
        }
    }else if(tam == 2){
           
     switch(romano[0]){
       case '1':
       System.out.print("X");
       break;
       case '2':
       System.out.print("XX");
       break;
       case '3':
       System.out.print("XXX");
       break;
       case '4':
       System.out.print("XL");
       break;
       case '5':
       System.out.print("L");
       break;
       case '6':
       System.out.print("LX");
       break;
       case '7':
       System.out.print("LXX" );
       break;
       case '8':
       System.out.print("LXXX");
       break;
       case '9':
       System.out.print("XC");
       break;
     }
     switch(romano[1]){
           case '1':
           System.out.print("I");
           break;
           case '2':
           System.out.print("II");
           break;
           case '3':
           System.out.print("III");
           break;
           case '4':
           System.out.print("IV");
           break;
           case '5':
           System.out.print("V");
           break;
           case '6':
           System.out.print("VI");
           break;
           case '7':
           System.out.print("VII" );
           break;
           case '8':
           System.out.print("VIII");
           break;
           case '9':
           System.out.print("IX");
           break;
        }
      }else if(tam == 3){
         
       switch(romano[0]){
        case '1':
        System.out.print("C");
        break;
        case '2':
        System.out.print("CC");
        break;
        case '3':
        System.out.print("CCC");
        break;
        case '4':
        System.out.print("CD");
        break;
        case '5':
        System.out.print("D");
        break;
        case '6':
        System.out.print("DC");
        break;
        case '7':
        System.out.print("DCC" );
        break;
        case '8':
        System.out.print("DCCC");
        break;
        case '9':
        System.out.print("DM");
        break;
      }
      switch(romano[1]){
       case '1':
       System.out.print("X");
       break;
       case '2':
       System.out.print("XX");
       break;
       case '3':
       System.out.print("XXX");
       break;
       case '4':
       System.out.print("XL");
       break;
       case '5':
       System.out.print("L");
       break;
       case '6':
       System.out.print("LX");
       break;
       case '7':
       System.out.print("LXX" );
       break;
       case '8':
       System.out.print("LXXX");
       break;
       case '9':
       System.out.print("XC");
       break;
     }
     switch(romano[2]){
           case '1':
           System.out.print("I");
           break;
           case '2':
           System.out.print("II");
           break;
           case '3':
           System.out.print("III");
           break;
           case '4':
           System.out.print("IV");
           break;
           case '5':
           System.out.print("V");
           break;
           case '6':
           System.out.print("VI");
           break;
           case '7':
           System.out.print("VII" );
           break;
           case '8':
           System.out.print("VIII");
           break;
           case '9':
           System.out.print("IX");
           break;
        }
    }else if(tam == 2){
           
     switch(romano[0]){
       case '1':
       System.out.print("X");
       break;
       case '2':
       System.out.print("XX");
       break;
       case '3':
       System.out.print("XXX");
       break;
       case '4':
       System.out.print("XL");
       break;
       case '5':
       System.out.print("L");
       break;
       case '6':
       System.out.print("LX");
       break;
       case '7':
       System.out.print("LXX" );
       break;
       case '8':
       System.out.print("LXXX");
       break;
       case '9':
       System.out.print("XC");
       break;
     }
     switch(romano[1]){
           case '1':
           System.out.print("I");
           break;
           case '2':
           System.out.print("II");
           break;
           case '3':
           System.out.print("III");
           break;
           case '4':
           System.out.print("IV");
           break;
           case '5':
           System.out.print("V");
           break;
           case '6':
           System.out.print("VI");
           break;
           case '7':
           System.out.print("VII" );
           break;
           case '8':
           System.out.print("VIII");
           break;
           case '9':
           System.out.print("IX");
           break;
        }
    }else if(tam == 1){
           
          switch(romano[0]){
           case '1':
           System.out.print("I");
           break;
           case '2':
           System.out.print("II");
           break;
           case '3':
           System.out.print("III");
           break;
           case '4':
           System.out.print("IV");
           break;
           case '5':
           System.out.print("V");
           break;
           case '6':
           System.out.print("VI");
           break;
           case '7':
           System.out.print("VII" );
           break;
           case '8':
           System.out.print("VIII");
           break;
           case '9':
           System.out.print("IX");
           break;
        }
     }else if(tam == 0 ){
           
           System.out.println();
     } 
  }
   
}


Saludos.................. Cool
« Última modificación: Marzo 15, 2008, 02:58:44 por [p3ll3] » En línea


Necesitas ser usuario para ver los enlaces Crear Usuario  Hacer Sesion


Necesitas ser usuario para ver los enlaces Crear Usuario  Hacer Sesion

__Horus__
Recien Llegado
*
Desconectado Desconectado

Mensajes: 2


Ver Perfil Email
« Respuesta #3 en: Marzo 15, 2008, 09:18:20 »

gracias men si tambien me decian que estaba mal el subindice y me decian que seria mejor  agregarlo metodos con bucles para hicieran ya la impresion en vez de tanto switch y case pero si ya vi  en que andaba mal  Smiley thanks
En línea
Páginas: [1] Ir Arriba Imprimir 
Comunidad Underground Hispana  |  Programacion  |  Programacion para webmasters  |  Java - Java Script (Moderador: -rOnNy-)  |  Tema: Duda con arreglo de numeros romanos « anterior próximo »
Ir a:  


Ranking-Hits
Powered by SMF 1.1.7 | SMF © 2006-2007, Simple Machines LLC