如何解决解析问题?

时间:2016-10-04 13:11:00

标签: java parsing

我正在使用jdk se并且我不断收到解析问题

“错误:解析”

时到达文件末尾

我真的不明白为什么我一直都会遇到这个问题 我让我的班级关闭,括号在我认为我需要括号的地方。

import java.util.Scanner;

public class days_in_a_month {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Please enter a month:");
String month = input.nextLine();
input.nextLine();

System.out.print("Please enter a year:");
String year = input.nextLine();
input.nextLine();

  boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0); 

    switch (month){
    case "1":
    case "3":
    case "5":
    case "7":
    case "8":
    case "10":
    case "12":
        System.out.println(month + " " + year + " has 31 days"); break;

    case "4":
    case "6":
    case "9":
    case "11":
        System.out.println(month + " " + year + " has 30 days"); break;

    case "2":
        if(isLeapYear)
        {
            System.out.println(month + " " + year + " has 29 days"); break;
        }
            else
        {
            System.out.println(month + " " + year + " has 28 days");
            }
        }
  }

4 个答案:

答案 0 :(得分:2)

解决的最佳方法是使用以下方法:

int year = input.nextInt();

答案 1 :(得分:0)

只需将变量定义为 int

int year = Integer.valueOf(input.nextLine());

问题是您不能对字符串使用%运算符

以下是完整示例:

package com.yourpackage;


import java.util.Scanner;

public class days_in_a_month {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter a month:");
        String month = input.nextLine();
        input.nextLine();

        System.out.print("Please enter a year:");
        int year = Integer.valueOf(input.nextLine());
        input.nextLine();

        boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

        switch (month) {
            case "1":
            case "3":
            case "5":
            case "7":
            case "8":
            case "10":
            case "12":
                System.out.println(month + " " + year + " has 31 days");
                break;

            case "4":
            case "6":
            case "9":
            case "11":
                System.out.println(month + " " + year + " has 30 days");
                break;

            case "2":
                if (isLeapYear) {
                    System.out.println(month + " " + year + " has 29 days");
                    break;
                } else {
                    System.out.println(month + " " + year + " has 28 days");
                }
        }
    }
}

请勿忘记将文件命名为" days_in_a_month "并在文件顶部定义适当的

答案 2 :(得分:0)

在您的代码中,您尝试将%运算符与String

一起使用
boolean isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

运算符%不能与字符串

一起使用

答案 3 :(得分:0)

最佳解决方案是:

package com.dayinmonth

import java.time.LocalDate;
import java.util.Scanner;

public class days_in_a_month {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter a month:");
        int month = input.nextInt();
        input.nextLine();

        System.out.print("Please enter a year:");
        int year = input.nextInt();
        input.nextLine();

        LocalDate date = LocalDate.of(year, month, 1);

        System.out.println(String.format("%d %d has %d days", date.getMonthValue(), date.getYear(), date.lengthOfMonth()));

    }
}