0

What I'm assigned to do is create an object-oriented validator. The user is prompted to input an integer, and the application validates it. The end result will display on the console as follows (first 3 inputs being invalid, 4th being valid):


Welcome to the Validation Tester application

Int Test

Enter an integer between -100 and 100: X

Error! Invalid integer value. Try again.

Enter an integer between -100 and 100: -101

Error! Number must be greater than -101

Enter an integer between -100 and 100: 101

Error! Number must be less than 101

Enter an integer between -100 and 100: 100


I've been assigned to create a validation class before but never in the way I'm being asked to now. Before, I've been able to pass the sc and the prompt to the Validation class and have the methods process them accordingly. For example:

    //MAIN 
    Scanner sc = new Scanner(System.in);
    int x = Validator.getInt(sc, "Enter an integer:     ", 0, 1000);

    //VALIDATION CLASS
    public class Validator{ 

    public static int getInt(Scanner sc, String prompt)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextInt())
            {
                i = sc.nextInt();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid integer value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return i;
    }

    public static int getInt(Scanner sc, String prompt,
    int min, int max)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            i = getInt(sc, prompt);
            if (i <= min)
                System.out.println(
                    "Error! Number must be greater than " + min + ".");
            else if (i >= max)
                System.out.println(
                    "Error! Number must be less than " + max + ".");
            else
                isValid = true;
        }
        return i;
    }

Done as above, I understand what is happening.

However now I'm assigned get the same results using similar methods but this time the sc has its own constructor.


public class OOValidator 
{
    public OOValidator(Scanner sc){}
    public int getInt(String prompt){}
    public int getIntWithinRange(String prompt, int min, int max){}
}

I'm not asking anyone to do the assignment for me in its entirety, but I'm at a loss as to how I can both prompt the user and pass the user's input using a class that has the sc and prompt separated.

I've tried several to code it several difference ways, non of which compile.

2
  • Please use more java like formatting - your code is difficult to read by java programmers as it is - usual style is brace on the same line Oct 12, 2014 at 21:08
  • Brace on the same line? Can you show me an example. I tried to make it as simple as possible. Oct 12, 2014 at 21:21

1 Answer 1

0

Just create an instance of your class

//MAIN 
Scanner sc = new Scanner(System.in);
OOValidator val = new OOValidator(sc);
int x = val.getInt("Enter an integer:     ");
// ...
int y = val.getIntWithinRange("Enter an integer:     ", 0, 1000);

//VALIDATION CLASS
public class OOValidator {

    private Scanner sc;
    private static final String ERROR = "Error! Invalid integer value." 
                                            +  "Try again.";
    public OOValidator(Scanner sc) {
        this.sc = sc;
    }

    public int getInt(String prompt) {
        while (true) {
            System.out.print(prompt);
            if (sc.hasNextInt()) {
                i = sc.nextInt();
                sc.nextLine();  // discard any other data entered on the line
                break;
            } else {
                System.out.println(ERROR);
                sc.nextLine();  // discard any other data entered on the line
            }
        }
        return i;
    }

    public int getIntWithinRange(String prompt, int min, int max) {
        // same logic - use directly sc which is an instance field
    }
}
4
  • int x = new OOValidator(sc).getInt(sc, "Enter an integer: "); This works with one minor edit, I removed sc and only passed the prompt. Nonetheless, I didn't realize I could do that. What does this syntax say exactly? Oct 12, 2014 at 21:27
  • @DeuceCartier: yep this was a typo - what this does is create an instance of the OOValidator class (an OOValidator object) and call its methods - edited for clarity Oct 12, 2014 at 22:01
  • Im realizing that in your edit, you've changed it so the OOValidator(sc) is created first, then the method is called by appending it to the variable. That's interesting, because the initial syntax also worked, where you had it all in one line. Oct 12, 2014 at 22:20
  • @DeuceCartier: Yes - with the initial syntax I created an object to just call a method on it - then the object would be discarded - usually you want to keep the object in a variable to reuse it as I done in my edit. Oct 12, 2014 at 22:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.