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

对着背影说爱祢 提交于 2019-12-11 01:54:50

问题


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:

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]);

回答1:


Change:

String date = Scanner.next();  

to:

String date = keyboard.next();  

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

Also, change:

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

to:

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

the split() method returns a string array.




回答2:


You mean

String date = keyboard.next();

instead of

String date = Scanner.next();



回答3:


Change the code:

String date=Scanner.next();

to:

String date = keyboard.next();



回答4:


It is Integer.parseInt() not IntegerParseInt.

Also change to this

String date = keyboard.next();



回答5:


It should be keyboard.next() not Scanner.next().



来源:https://stackoverflow.com/questions/9204241/non-static-method-next-cannot-be-referenced-from-a-static-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!