Converting string to long int

Asked

Viewed 279 times

5

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

I wonder if in Java there is any function that does this... I searched in some places, but I did not find anything that could help me until then.

The code I made is just printing the number as a character without this conversion that the problem speaks of.

    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();
}
  • 1

    Show what you have done so far by clicking EDIT and adding the question.

  • 1

    There is nothing ready in Java (it may be that someone created something outside). The problem here is the definition of the problem. Without knowing exactly what the problem is, there is no way to find the right solution. What do you really want to do? Create a function that takes a number in full and returns the same number (long)? Whether to return long then you can’t go from 1 to 10. If so, the algorithm is absurdly more complex than what you’re doing, and you can’t start helping yourself. If you want to know how to return the number instead of printing, then it’s easy, but I doubt that this alone will solve.

  • "Implement a class that takes the first ten numbers in full (from "one" to "ten") and converts them into integers (long int). Use conditional switch for conversion" This is the proposed problem

2 answers

5


I find it strange to ask for a long for that, but come on.

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

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

import java.util.*;

class Main {
    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();
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Gee, thanks anyway... Now I think it made sense. I made a mistake in the interpretation of the same statement. But thanks, gave a light here.

  • A silly question arose here, inside the switch cases, I’m not seeing the break, since it has a kind of return it dispenses with the use of break?

  • 1

    It’s not because of the kind of feedback, it’s because he has a return inside it. This command lets you exit the method at that moment, if you had a break there then when he would be executed?

  • I got it, since Return needs to be the last command it terminates, thanks.

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);
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.