1

I want to create a condition for the variable "int edad" but I'm confused...

I need a condition to this line in particular

Integer.parseInt(JOptionPane.showInputDialog("Ingrese edad")),

this is a int variable and I want to verify that is between 0 and 100, then give a message to the user and ask again this variable.

package pruebas;

import javax.swing.JOptionPane;

public class Persona {

public static void main(String[] args) {
    Estudiante [] misEstudiantes2 = new Estudiante[1];

    for(int i = 0; i< misEstudiantes2.length; i++){

        misEstudiantes2[i] = new Estudiante(JOptionPane.showInputDialog("Ingrese nombre").toUpperCase(), 
           Integer.parseInt(JOptionPane.showInputDialog("Ingrese edad")),
           JOptionPane.showInputDialog("Ingrese carrera"),
           Integer.parseInt(JOptionPane.showInputDialog("Ingrese año")),
           Integer.parseInt(JOptionPane.showInputDialog("Ingrese mes")),
           Integer.parseInt(JOptionPane.showInputDialog("Ingrese dia")),
           Double.parseDouble(JOptionPane.showInputDialog("Ingrese sueldo")));

        misEstudiantes2[i].estableceSueldo(1);

        System.out.println("Objetos del array \n" + misEstudiantes2[i].dameNombre() + "\n" + misEstudiantes2[i].dameSueldo() + "\n" +
        misEstudiantes2[i].dameCarrera() + "\n" + misEstudiantes2[i].dameFecha() + "\n" + "La edad es " + misEstudiantes2[i].dameEdad());
    }
}
}
1
  • Welcome to stackoverflow! Please use English in code of question too. People are replying partially in your native language, which is nice of them, but potentially not useful to other readers. If you prefer there are a couple of other language versions of stackoverflow: meta.stackoverflow.com/a/297734/360211 Commented Jan 6, 2017 at 14:28

3 Answers 3

1

This is possible if you store all the user input into variables before constructing the new student object.

String name = JOptionPane.showInputDialog("Ingrese nombre").toUpperCase();
int age = Integer.parseInt(JOptionPane.showInputDialog("Ingrese edad"));
String career = JOptionPane.showInputDialog("Ingrese carrera");
int year = Integer.parseInt(JOptionPane.showInputDialog("Ingrese año"));
int month = Integer.parseInt(JOptionPane.showInputDialog("Ingrese mes"));
int day = Integer.parseInt(JOptionPane.showInputDialog("Ingrese dia"));
double salary = Double.parseDouble(JOptionPane.showInputDialog("Ingrese sueldo")));

misEstudiantes2[i] = new Estudiante(name, age, career, year, month, day, salary);

From here, you can put any checks necessary around your variables. For example:

int age = Integer.parseInt(JOptionPage.showInputDialog("Ingrese edad"));
while (age < 0 || age > 100) { // in case they put in an invalid number
                               // try again, with a notice about the range
    age = Integer.parseInt(JOptionPage.showInputDialog("Ingrese edad, mas de 0 y menos de 100"));
}
Sign up to request clarification or add additional context in comments.

Comments

1

Do the user a favour and offer them a spinner to select the number.

import javax.swing.*;

class VerifiedIntegerInRange {

    public VerifiedIntegerInRange() {
        SpinnerNumberModel numberRange = new SpinnerNumberModel(-1,-1,100,1);
        JSpinner spinner = new JSpinner(numberRange);
        int i = numberRange.getNumber().intValue();
        while (i<0) {
            JOptionPane.showMessageDialog(
                null,
                spinner,
                "Select a number between 0 & 100",
                JOptionPane.QUESTION_MESSAGE);
            i = numberRange.getNumber().intValue();
        }
        System.out.println("User chose " + i);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                new VerifiedIntegerInRange();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Comments

1

Try writing a method that does that for you. For example:

public static Integer promptForInteger(int min, int max, String message, String message2) {
  Integer result = Integer.parseInt(JOptionPane.showInputDialog(message));
  while(result < min || result > max) {
    result = Integer.parseInt(JOptionPane.showInputDialog(message2));
  }
  return result;
}

Then call it when you create your object:

misEstudiantes2[i] = new Estudiante(JOptionPane.showInputDialog("Ingrese nombre").toUpperCase(),
                                    promptForInteger(0, 100, "Ingrese edad", "Ingrese edad, mas de 0 y menos de 100"),
                                    JOptionPane.showInputDialog("Ingrese carrera"),
                                    Integer.parseInt(JOptionPane.showInputDialog("Ingrese año")),
                                    Integer.parseInt(JOptionPane.showInputDialog("Ingrese mes")),
                                    Integer.parseInt(JOptionPane.showInputDialog("Ingrese dia")),
                                    Double.parseDouble(JOptionPane.showInputDialog("Ingrese sueldo")));

I dont know your native language so i used the strings from johnhopkinses answer.

5 Comments

Why not put the condition in the while: while(result < min || result > max)? But also protect against min>max!
Fair point with the condition. I disagree on the min>max check. It serves no purpose as the author is the only user of this api and the parameters are well named.
The purpose would be to prevent entering an infinite loop. Note, the second Integer result = must be just result =
Corrected. So what do you suggest? One could swap the values or throw an IllegalArgumentException.
I'd think I'd prefer IllegalArgumentException. But you might even say not asking the user at all and defaulting to the minimum is reasonable. I'd say it doesn't matter so much as long as it's documented behavior, ideally covered in a test case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.