How to make the program continue where I left off after an exception happens in my Ry

Asked

Viewed 65 times

-1

In this excerpt I got the expected result, which was not close the program after the error message if the user enters with wrong data. However, even if I’m in the 10th, it resets to the first. So here comes the question, is there anything I can do in this excerpt so that only ask the die again from pm with invalid die?

//Entrada de dados
while(true) { //Solução para o programa não fechar após mensagem de erro
    try //Inicio trycatch
    {
        pm1 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 1ª mala? \n Insira abaixo:"));
        pm2 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 2ª mala? \n Insira abaixo:"));
        pm3 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 3ª mala? \n Insira abaixo:"));
        pm4 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 4ª mala? \n Insira abaixo:"));
        pm5 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 5ª mala? \n Insira abaixo:"));
        pm6 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 6ª mala? \n Insira abaixo:"));
        pm7 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 7ª mala? \n Insira abaixo:"));
        pm8 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 8ª mala? \n Insira abaixo:"));
        pm9 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 9ª mala? \n Insira abaixo:"));
        pm10 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 10ª mala? \n Insira abaixo:"));
        break;
    } catch (NumberFormatException e) 
    {
        JOptionPane.showMessageDialog ( null, "Digite um peso válido!\n"
            + "Tente usar .(ponto) para números decimais.\n"
            + "Não use letras, apenas números.");
    }
}
  • This code is being made unprofessional. Why not store the number of bags in an integer array and then with a "foreach" cycle, input the values? It was more professional code. If you have questions I can post an example

  • @Barrosking I understand, is that I am starting in the course, it is only the fifth week, the challenge in question was reaching, with the tools that we have learned so far, is that I would like to improve this question of the reset of values, and I do not quite know how to do this, because I had to put 10 variables for guards these pms, being that I imagine that a counter would be more efficient.

  • An answer was placed with the purpose spoken by me, if you do not understand anything you may ask!

1 answer

3


Create a 10 position arrangement first instead of doing this.

float pm = new float [10]

Now just walk.

for(int i = 0; i < pm.length; i++)
{
    try
    {
        pm[i] = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a" + (i+1) "ª mala? \n Insira abaixo:"));
    } catch (NumberFormatException e) {
        i--; //Aqui irá diminuir o valor de i para que a linha seja repetida
        JOptionPane.showMessageDialog ( null, "Digite um peso válido!\n"
            + "Tente usar .(ponto) para números decimais.\n"
            + "Não use letras, apenas números.")
    }
}
  • 1

    Thank you here worked perfectly.

Browser other questions tagged

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