本文介绍了不能从静态上下文中引用非静态方法 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()
发布评论