不能从静态上下文中引用非静态方法 next() [英] Non-static method next() cannot be referenced from a static context

查看:58
本文介绍了不能从静态上下文中引用非静态方法 next()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 mm/dd/yyyy 格式的日期解析为单独的字段,但在尝试编译时出现以下错误:

I am trying to parse out a mm/dd/yyyy formatted date into separate fields, but I get the following error when I try to compile:

不能从静态上下文中引用非静态方法 next()

non-static method next() cannot be referenced from a static context

可能导致错误的原因是什么?

What could be causing the error?

import java.util.Scanner;

public class Problem39
{

    public static void main(String [ ] args)
    {

    boolean isLeapYear =false;
    int maxDay=0;
    String stringDate;

    System.out.println("Enter the date in mm/dd/yyyy format. ");  //user input
    Scanner keyboard = new Scanner(System.in);                    //read input
    String date=Scanner.next();                                //store input
    String temp=date.split("/");  //parse date
    int month=IntegerParseInt(temp[1]);
    int day=IntegerParseInt(temp[0]);
    int year=IntegerParseInt(temp[2]);

推荐答案

更改:

String date = Scanner.next();  

到:

String date = keyboard.next();  

next() 是一个实例方法,因此您必须在类 Scanner 的实例上调用它.

next() is an instance method, so you must call it on an instance of the class Scanner.

另外,更改:

String temp = date.split("/"); 

到:

String[] temp = date.split("/"); 

split() 方法返回一个字符串数组.

the split() method returns a string array.

这篇关于不能从静态上下文中引用非静态方法 next()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆