-2

I'm working with two classes. One is called Validator and one is called LineItemApp.

Here is the code for the Validator class.

import java.util.Scanner;

public class Validator
{
public String getString(Scanner sc, String prompt)
{
    System.out.print(prompt);
    String s = sc.next();  // read user entry
    sc.nextLine();  // discard any other data entered on the line
    return s;
}

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

public double getDouble(Scanner sc, String prompt)
{
    double d = 0;
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.print(prompt);
        if (sc.hasNextDouble())
        {
            d = sc.nextDouble();
            isValid = true;
        }
        else
        {
            System.out.println("Error! Invalid decimal value. Try again.");
        }
        sc.nextLine();  // discard any other data entered on the line
    }
    return d;
}

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

Here is my LineItemApp class

    import java.util.Scanner;

public class LineItemApp
{
public static void main(String args[])
{
    // display a welcome message
    System.out.println("Welcome to the Line Item Calculator");
    System.out.println();

    // create 1 or more line items
    Scanner sc = new Scanner(System.in);
    String choice = "y";
    while (choice.equalsIgnoreCase("y"))
    {
        // get the input from the user
        String productCode = Validator.getString(sc,
            "Enter product code: ");
        int quantity = Validator.getInt(sc,
            "Enter quantity:     ", 0, 1000);

        // get the Product object
        Product product = ProductDB.getProduct(productCode);

        // create the LineItem object
        LineItem lineItem = new LineItem(product, quantity);

        // display the output
        System.out.println();
        System.out.println("LINE ITEM");
        System.out.println("Code:        " + product.getCode());
        System.out.println("Description: " + product.getDescription());
        System.out.println("Price:       " + product.getFormattedPrice());
        System.out.println("Quantity:    " + lineItem.getQuantity());
        System.out.println("Total:       " +
            lineItem.getFormattedTotal() + "\n");

        // see if the user wants to continue
        choice = Validator.getString(sc, "Continue? (y/n): ");
        System.out.println();
    }
}

}

Any help would be greatly appreciated.

Thanks, David

3 Answers3

2

You need an instance of Validator, to enter the methods

// see if the user wants to continue
    Validator validator = new Validator();
    choice = validator.getString(sc, "Continue? (y/n): ");
    System.out.println();

Or as suggested in another answer you can make Validator methods static, but this change sure is bigger and you have to change code, and you don't know if you use this validator in another part so im not agree but if you want to refactor it, is not at all bad idea cause your class Validator doesn't have state, only do some algorithms.

nachokk
  • 14,363
  • 4
  • 24
  • 53
  • Thanks a lot. Do I do the same thing for where it says String productCode = Validator.getString(sc, "Enter product code: "); And also at int quantity = Validator.getInt(sc, "Enter quantity: ",0,1000); I read that int was somehow different when it came to creating and instance. This is new to me and I'm having a hard time grasping it. – user2589551 Jul 17 '13 at 01:58
  • yep, you need an object to access methods non static, if you put `Validator.someMethod();` means that the class Validator has a `class level method` (static). – nachokk Jul 17 '13 at 02:01
  • Ok. I'm still lost on trying to do the same thing to String productCode = Validator.getString(sc, "Enter product code: ") It keeps giving me an error when I try to change it. – user2589551 Jul 17 '13 at 02:07
  • @user2589551 you have to define `Validator validator = new Validator();` top of things, java reads up to down ;) – nachokk Jul 17 '13 at 02:08
  • Haha I should have known that -_-. Anyway, is it the same way with an int variable? – user2589551 Jul 17 '13 at 02:17
  • @user2589551 don't understand , if you mean about declaring first using later? yes, first you have to declare a variable to use it in a method. – nachokk Jul 17 '13 at 02:18
  • Ok, thanks for all of your help. If you wouldn't care to help me on one more thing. I have to add a constructor to the validator class that takes no arguments. It looks pretty simple, but my syntax is off. – user2589551 Jul 17 '13 at 02:26
  • by default is always there implicit but if you want to make something inside you declare `public Validator(){ //code here} ;` – nachokk Jul 17 '13 at 02:28
1

You are calling non-static methods from a static method (which is now main() ).

You can eliminate the error by declaring the methods arising the warning to static.

Black Maggie
  • 496
  • 2
  • 15
0

Your should understand what is the static method. http://www.leepoint.net/notes-java/flow/methods/50static-methods.html

WindyYang
  • 151
  • 1
  • 3