How to convert a variable int to vector?

0

I've been trying to do this problem but I can not finish it enter int and write it in letter Example
150 == one hundred fifty

My biggest problem is that I get compile error when trying to print the vectors of numbers, I guess that is because the control variable ( aux1 ) is not int , or I did not know how to convert

This is my breakthrough:

String unidades [] = {"uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve"};  
        String decenas []= {"diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa"};  
        String centenas []= {"ciento", "docientos", "trecientos", "cuatrocientos", "quinientos", "seicientos", "setecientos", "ochocientos", "novecientos"};  
        String unidad_m []= {"mil", "dosmil", "tresmil", "cuatromil", "cincomil", "seismil", "sietemil", "ochomil", "nuevemil"};  
        String decena_m []= {"once", "doce", "trece", "catorce", "quince", "dieciseis", "dieciciete", "dieciocho", "diecinueve"};  
        int num, aux1;  
        char aux;  
        System.out.println("escribe un numero");  
        num = var.nextInt();  
        String numero, numero2;  
        numero=Integer.toString(num);  
        numero2 = numero;
        char vec [] = numero2.toCharArray();  
        //una disculpa, hay algunas cariables basura  
        for (int i = vec.length-1; i > -1; i--) {
            aux = vec [i];
            aux1 = (int)aux;
            System.out.println(aux);
            if(vec.length==4){
                System.out.print(decena_m[aux1]);
            }else{
                if(vec.length==3){
                System.out.print(unidad_m[aux1]);
                }else{
                    if(vec.length==2){
                    System.out.print(centenas[aux1]);
                    }else{
                        if(vec.length==1){
                        System.out.print(decenas[1]);
                        }else{
                            if(vec.length==0){
                            System.out.print(unidades[aux1]);
                            }
                        }
                    }
                }
            }
        }
    
asked by Fernando Antonio 17.09.2018 в 07:00
source

3 answers

1

effectively one of the problems is that aux1 is not the number you want, do not parsea well. If you do this, it will parse well: Character.getNumericValue(aux) , apart you have to subtract 1, since in your arrays the numbers start at 0.

  

aux1 = Character.getNumericValue (aux) -1;

On the other hand, in the if you must compare against i. At this point, it would only be necessary to break the number upside down.

An example of operation:

for (int i = 0; i < vec.length; i++) {
        aux = vec [i];
        aux1 = Character.getNumericValue(aux)-1;
        if(i==-1){
            System.out.print(decena_m[aux1]+ ' ');
        }else{
            if(i==0){
            System.out.print(unidad_m[aux1]+ ' ');
            }else{
                if(i==1){
                System.out.print(centenas[aux1]+ ' ');
                }else{
                    if(i==2){
                    System.out.print(decenas[aux1]+ ' ');
                    }else{
                        if(i==3){
                        System.out.print(unidades[aux1]);
                        }
                    }
                }
            }
        }
    }
    
answered by 17.09.2018 в 09:04
1

Your program has several problems:

  • The first one is when going through the array of vectors, you start at the end ( 589 = nueve ochenta quinientos )
  • The second is that you can not cast (int) of char , it will not give you a correct result, use Character.getNumericValue(vec[i]) - 1 .
  • All if else without mistakes, if(vec.length==0) can never happen, this means that you have not inserted any value! if I put you 587 vec.length = 3 but your if it prints for case 3 "unit_m" so I would print cincomil, when it should be five hundred.
  • Here is your functional and improved program without VARIABLES TRASH XD.

        import java.util.Scanner;
    
        public class Test {
    
        public static void main(String[] args) {
    
            String unidades [] = {"uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve"};  
            String decenas []= {"diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa"};  
            String centenas []= {"ciento", "docientos", "trecientos", "cuatrocientos", "quinientos", "seicientos", "setecientos", "ochocientos", "novecientos"};  
            String unidad_m []= {"mil", "dosmil", "tresmil", "cuatromil", "cincomil", "seismil", "sietemil", "ochomil", "nuevemil"};  
            String decena_m []= {"once", "doce", "trece", "catorce", "quince", "dieciseis", "dieciciete", "dieciocho", "diecinueve"};  
    
            Scanner leer = new Scanner(System.in);
            System.out.println("Escribe un numero: ");  
            char vec [] = leer.next().toCharArray();
            int currentVectorValue=0; 
    
            for (int i = 0; i < vec.length; i++) {
                // Tus arrays de unidades centenas.. empiezan todos en 1 (diez, cien) 
                // pero el array se inicializa en 0, por lo que le quitamos una unidad 
                // al valor recuperado, EXAMPLE: si recuperamos un numero 1, 
                // vamos al vector unidades[0] ya que en la posición 0 está el valor 1
                currentVectorValue = Character.getNumericValue(vec[i]) - 1;
    
                // Para saber si es unidade decenas centenas etc : vec.length - i
                switch (vec.length - i) {
                case 5:
                    System.out.print(decena_m[currentVectorValue]);
                    break;
                case 4:
                     System.out.print(unidad_m[currentVectorValue]);
                    break;
                case 3:
                    System.out.print(centenas[currentVectorValue]);
                    break;
                case 2:
                    System.out.print(decenas[currentVectorValue]);
                    break;
                case 1:
                    System.out.print(unidades[currentVectorValue]);
                    break;
                default:
                    System.out.print("Out of range");
                }
    
                System.out.print(" ");
            }
        }
    }
    
        
    answered by 17.09.2018 в 09:07
    1

    I will provide my answer because the others do not take into account when it comes to numbers that end in tens (11, 12, etc. print "ten one", "ten two", etc.)

    That is the only case that I deal with in a particular way inside the loop. For the rest, I changed the if's for a switch that I think is much cleaner, apart from using the correct condition that I can use to know which digit I'm dealing with in the iteration.

    By keeping a certain similarity with what you already have, I will not over modify your code, although it can be improved:

    String unidades[] = { "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve" };
    String decenas[] = { "diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa" };
    String centenas[] = { "ciento", "docientos", "trecientos", "cuatrocientos", "quinientos", "seicientos", "setecientos", "ochocientos", "novecientos" };
    String unidad_m[] = { "mil", "dosmil", "tresmil", "cuatromil", "cincomil", "seismil", "sietemil", "ochomil", "nuevemil" };
    String decena_m[] = { "diez", "once", "doce", "trece", "catorce", "quince", "dieciseis", "dieciciete", "dieciocho", "diecinueve" };
    
    System.out.println("escribe un numero");
    int num = var.nextInt();
    String numStr = Integer.toString(num);
    for (int i = 0; i < numStr.length(); i++) {
        int aux = Integer.parseInt(numStr.substring(i, i + 1));
        if (aux == 0) continue;
    
        if ((numStr.length() - i) == 2) {
            if (aux == 1) {
                aux = Integer.parseInt(numStr.substring(i, i + 2));
                System.out.print(decena_m[aux - 10]);
                break;
            }
        }
    
        switch (numStr.length() - i - 1) {
        case 3:
            System.out.print(unidad_m[aux - 1]);
            break;
        case 2:
            System.out.print(centenas[aux - 1]);
            break;
        case 1:
            System.out.print(decenas[aux - 1]);
            break;
        case 0:
            System.out.print(unidades[aux - 1]);
            break;
        }
        System.out.print(" ");
    }
    
        
    answered by 17.09.2018 в 09:20