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

编程入门 行业动态 更新时间:2024-10-10 08:21:14
本文介绍了不能从静态上下文中引用非静态方法 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()

本文发布于:2023-11-16 03:48:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1600721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   文中   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!