【问题标题】:How do I get the error "non-static method GetString(Scanner,String) cannot be refereneced from a static context" to go away? [duplicate]如何让错误“无法从静态上下文中引用非静态方法 GetString(Scanner,String)”消失? [复制]
【发布时间】:2013-07-17 01:44:46
【问题描述】:

我正在处理两个班级。一种称为 Validator,一种称为 LineItemApp。

这是 Validator 类的代码。

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

这是我的 LineItemApp 类

    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();
    }
}

}

任何帮助将不胜感激。

谢谢, 大卫

【问题讨论】:

  • 这类问题大概是在stackoverflow.com/questions/14862306/…之前回答的
  • 要么调用静态方法,要么从非静态上下文中引用它。
  • (或者,至少指出哪一行产生了错误。)

标签: java


【解决方案1】:

您需要一个 Validator 实例来输入方法

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

或者按照另一个答案中的建议,您可以将验证器方法设为静态,但这种更改肯定更大,您必须更改代码,而且您不知道您是否在另一部分使用此验证器,所以我不同意,但如果您想要重构它,一点也不坏,因为你的类Validator没有状态,只做一些算法。

【讨论】:

  • 非常感谢。我是否对 String productCode = Validator.getString(sc, "Enter product code: "); 执行相同的操作?而且在 int quantity = Validator.getInt(sc, "Enter quantity: ",0,1000);我读到 int 在创建和实例方面有所不同。这对我来说是新的,我很难掌握它。
  • 是的,你需要一个对象来访问非静态方法,如果你把Validator.someMethod(); 表示类Validator 有一个class level method(静态)。
  • 好的。我仍然迷失在尝试对 String productCode = Validator.getString(sc, "Enter product code: ") 做同样的事情时,当我尝试更改它时它一直给我一个错误。
  • @user2589551 你必须定义Validator validator = new Validator(); 最重要的是,java 从上到下读取;)
  • 哈哈我应该知道-_-。无论如何,它与 int 变量的方式相同吗?
【解决方案2】:

您正在从静态方法(现在是 main() )调用非静态方法。

您可以通过将产生警告的方法声明为静态来消除错误。

【讨论】:

  • 问题是他没有Validator的实例或者getString不是静态的..
【解决方案3】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多