Converting string to long int

4

I have an activity to do in which the teacher asks that the names of the numbers be converted into integers ( long int ).

I would like to know if there is a function in Java that does this ... I searched in some places, but found nothing that could help me until then.

The code I've done is just printing the number as a character without this conversion that the problem talks about.

    public static void converte(String entrada) {

    switch (entrada) {

    case "um":
        System.out.println("1");
        break;
    case "dois":
        System.out.println("2");
        break;
    case "três":
        System.out.println("3");
        break;
    case "quatro":
        System.out.println("4");
        break;
    case "cinco":
        System.out.println("5");
        break;
    case "seis":
        System.out.println("6");
        break;
    case "sete":
        System.out.println("7");
        break;
    case "oito":
        System.out.println("8");
        break;
    case "nove":
        System.out.println("9");
        break;
    case "dez":
        System.out.println("10");
        break;
    }

}
public static void main(String[] args) {
    Scanner key = new Scanner(System.in);

    System.out.println(
            "Inicialização...\nInstruções:\n1-Digite o nome de um número entre um~dez.\n2-O programa encerra ao digitar 'fim'.");

    while (true) {
        String entrada = key.nextLine();

        if (entrada.equals("fim")) {
            break;
        }

        converte(entrada);

    }

    System.out.println("Fim!");

    key.close();
}
    
asked by anonymous 18.11.2017 / 14:04

2 answers

4

I find it odd to ask a long for this, but come on.

By the statement the function should return a long and not a void . And you should not print anything, just return the value, and can already be an integer, you do not have to use a character with the digit.

It is also good to treat the case of being typed something invalid, so I created default .

import java.util.*;

class Conversao {
    public static long converte(String entrada) {
        switch (entrada) {
        case "um":
            return 1;
        case "dois":
            return 2;
        case "três":
            return 3;
        case "quatro":
            return 4;
        case "cinco":
            return 5;
        case "seis":
            return 6;
        case "sete":
            return 7;
        case "oito":
            return 8;
        case "nove":
            return 9;
        case "dez":
            return 10;
        default:
            return -1;
        }
    }
    public static void main(String[] args) {
        Scanner key = new Scanner(System.in);
        System.out.println("Inicialização...\nInstruções:\n1-Digite o nome de um número entre um~dez.\n2-O programa encerra ao digitar 'fim'.");
        while (true) {
            String entrada = key.nextLine();
            if (entrada.equals("fim")) {
                break;
            }
            long convertido = converte(entrada);
            if (convertido == -1) {
                System.out.println("Palavra inválida");
                continue;
            }
            System.out.println(convertido);
        }
        System.out.println("Fim!");
        key.close();
    }
}

See running on ideone . And no Coding Ground . Also I placed in GitHub for future reference .

    
18.11.2017 / 14:43
0
private static final String letras = "um dois tres quatro cinco seis sete oito nove dez";
private static final long[] numeros = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

private static long converter(String entrada) {
    String string = Normalizer.normalize(entrada.toLowerCase(), Normalizer.Form.NFD).replaceAll("[^\p{ASCII}]",
            "");
    for (int i = 0; i < letras.split(" ").length; i++)
        if (string.equals(letras.split(" ")[i]))
            return numeros[i];
    return -1;
}

public static void main(String[] args) {
    System.out.println("Insira o valor:");
    Scanner scanner = new Scanner(System.in);
    while (true) {
        long valor = converter(scanner.nextLine());
        if (valor == -1) {
            System.out.println("Erro: numero invalido");
            scanner.close();
        } else {
            System.out.println("Convertido -> " + valor);
        }
    }
}
    
18.11.2017 / 21:44