0

I've tried merging the two loops into one do loop, but every time I enter an invalid value it doesn't prompt me the error message and ask to enter the value again. Instead it just moves on to the next prompt statement.

do {
    try {
        dependents = Integer.parseInt(JOptionPane.showInputDialog("number of dependents:"));
    }
    catch (NumberFormatException e) {
        dependents = MIN_DEPENDENTS - 1;
    }
    if (dependents < MIN_DEPENDENTS || dependents > MAX_DEPENDENTS) {
        JOptionPane.showMessageDialog(null, "Number of dependents must be between 0 and 9.");
    }
} while (dependents < MIN_DEPENDENTS || dependents > MAX_DEPENDENTS);

do {
    try {
        income = Double.parseDouble(JOptionPane.showInputDialog("amount of income:"));
    }
    catch (NumberFormatException e) {
        income = MIN_INCOME - 1;
    }
    if (income < MIN_INCOME || income > MAX_INCOME) {
        JOptionPane.showMessageDialog(null, "income must be between $0 and $999,999.");
    }
} while (income < MIN_INCOME || income > MAX_INCOME);
Tom
  • 16,842
  • 17
  • 45
  • 54
dan
  • 127
  • 9
  • 1
    @WhyCry That's two loops in sequence. – Bill the Lizard Jun 24 '15 at 13:35
  • 1
    @BilltheLizard I believe the issue is that OP doesn't want 2 loops but wants 1 loop that does everything the two loops did. – d0nut Jun 24 '15 at 13:36
  • 3
    I wouldn't merge them, just because they do different things as far as I can tell – Edwin Jun 24 '15 at 13:36
  • All i see you do in these "loops" is checking the users input. This should be done in a complete other way. For JOptionPane see here: http://stackoverflow.com/questions/13055107/joptionpane-check-user-input-and-prevent-from-closing-until-conditions-are-met – ceekay Jun 24 '15 at 13:37
  • 3
    There is no sense in merging these loops. – Rajesh Jun 24 '15 at 13:39
  • I'll just leave it as is. I was just trying to see I could improve on some efficiency by getting rid of the other do loop. – dan Jun 24 '15 at 14:01

3 Answers3

2

No, but you could create some sort of GetInput function and pass in min, max, promptText, and errorText. This would save you duplicating the code.

dependents = getInput(MIN_DEPENDENTS, MAX_DEPENDENTS,"number of dependents:","Number of dependents must be between 0 and 9.")
income = getInput(MIN_INCOME,MAX_INCOME,"amount of income:","income must be between $0 and $999,999.")

private double getInput(double min, double max, String promptText, String errorText) {
   double result = 0.0;
   do {
         try {
            result = Double.parseDouble(JOptionPane.showInputDialog(promptText));
         }
         catch (NumberFormatException e) {
            result = min - 1;
         }

         if (result < min || result > max) {
            JOptionPane.showMessageDialog(null, errorText);
         }
   } while (result < min || result > max);

   return result;
}
Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
0

Dan, you need to put below lined in your catch block to show error message and prompt again for input.

catch (NumberFormatException e) {        
                dependents = MIN_DEPENDENTS - 1; 
    JOptionPane.showMessageDialog(null, "Number of dependents must be between 0 and 9.");

    dependents = Integer.parseInt(JOptionPane.showInputDialog("number of dependents:"));
}
ManojP
  • 6,113
  • 2
  • 37
  • 49
0

Another option could to main list of input in array and then use it in while loop

Object[][] input = {
            {"number of dependents", MIN_DEPENDENTS, MAX_DEPENDENTS},
            {"amount of income", MIN_INCOME, MAX_INCOME},
    };

    int index = 0;
    int value, min, max;
    do {

        Object[] inputDetails = input[index];

        String label = inputDetails[0].toString();
        min = Integer.valueOf(inputDetails[1].toString());
        max = Integer.valueOf(inputDetails[2].toString());


        try {
            value = Integer.parseInt(JOptionPane.showInputDialog(label));
        } catch (NumberFormatException e) {
            value = min - 1;
        }
        if (value < min || value > max) {
            JOptionPane.showMessageDialog(null, String.format("%s must be between %s and %s", label, min, max));
        } else {
            index++;
        }

    } while ((value < min || value > max) || index < input.length);
Ashkrit Sharma
  • 627
  • 5
  • 7