扫描仪关闭或不读取?

问题描述 投票:0回答:2

我正在上课的家庭作业,而我遇到了一个奇怪的问题。当我运行代码时:

 import java.time.*;
import java.util.*;
public class story
{
    /*The main method calls the run method. do not add any other code to the main method*/
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);
        run(kb);

    }
    /*This method calls all other methods to process the data
    This is the only method that asks the user for the information such as full name, year born, the number to
    calculate the sqrt, the year that the userwants to know the easter day
    This is the only method that you can have print, println,printf*/
    private static void run(Scanner kb)
    {
        System.out.print("Hi, what is your name? ");
        String fullName = kb.nextLine();
        System.out.print("What year where you born? ");
        int age = kb.nextInt();

        lineBreak();

        System.out.println("OH you were born in " + age%100 + " and you are " + getAge(age) + " years old!!!");
        System.out.println("OH, I'm sorry " + getFirst(fullName) + ", how rude of me!");
        System.out.println("Let me introduce myself.");
        System.out.println("This is your computer speaking.");
        lineBreak();

        System.out.println("I need a good sounding name.");
        System.out.print("What do you think would be a good name for me? ");
        String machineName;
        machineName = kb.nextLine();
        lineBreak();

        System.out.println("Oh, " + machineName + " hmmm... that's good!");
        System.out.println("Please, let me start over.");
        lineBreak();

        System.out.println(getLast(fullName) + ", let me introduce myself.");
        System.out.println("I am " + getLast(machineName) + ", your computer. You can also call me " + getFirst(machineName));
        lineBreak();
        System.out.println(getFirst(fullName) + "you know what?");
        System.out.println("I can tell you what day Easter will be on");
        System.out.println("for any year (on the Gregorian calendar) from 1900 to 2099.");
        lineBreak();
        System.out.println("In " + age + ", the year you were born, Easter was on " + getEasterDay(age) +".");
        System.out.println("For what year are you interested in finding out");
        System.out.print("When Easter is? ");
        int testYear = kb.nextInt();
        System.out.println("In " + testYear + "Easter is on " + getEasterDay(testYear) + ".");
        lineBreak();
        System.out.println("Oh Maria I am really good at Math.");
        System.out.println("Give me any number and I can calculate the square root of that number,");
        System.out.print("what is your number: ");
        int testNumber = kb.nextInt();
        System.out.println("Square root of " + testNumber + " is: " + getEasterDay(testYear));
        lineBreak();
        System.out.println("Well " + getFirst(fullName) + ", it has been nice talking with you.");
        System.out.println("I am sure we will be spending a lot of time together this semester.");
        System.out.println("I hope you enjoy CSc15.");
        System.out.println("So long for now, have a nice day.");
    }

    //this method gets the user's full name and returns the first name. use methods from the String class
    //no print, println,printf in this method
    private static String getFirst(String fullName)
    {
        String[] parts = fullName.split(" ");
        return parts[0];
    }

    //This method gets the user's full name and returns the last name
    //no print, println,printf in this method
    private static String getLast(String fullName) {
        String[] parts = fullName.split(" ");
        return parts[1];
    }

    //this method gets the year that the user was born and returns the age of the user. How do you calculate the age?
    //no print, println,printf in this method
    private static int getAge(int year) {
        int current = Year.now().getValue();
        return current-year;
    }

    //this method gets the year and returns the easter  day for the given year.
    //no print, println,printf in this method
    private static String getEasterDay(int year)
    {
        int h = (24 + 19*(year % 19)) % 30;
        int i = h - h / 28;
        int j = (year + year/4 + i - 13) % 7;
        int k = i - j;
        int month = 3 + (k + 40)/44;
        int day = k + 28 - 31*(month/4);
        String date = month + "/" + day + "/" + year;
        return date;
    }

    //Repeat code for a line break
    private static void lineBreak(){
        System.out.print("\n");
    }
}

我收到此错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at story.getLast(story.java:78)
at story.run(story.java:42)
at story.main(story.java:9)

似乎扫描仪没有停止读取我的输入。这是一个简单的程序,可以执行多项操作,输入全名,出生年份,报告年龄,为计算机命名,通过提供的公式计算任何给定年份的复活节并对数字执行sqrt函数。

教授不希望我们使用.next做笔记。任何帮助或见识都将受到欢迎

java arrays java.util.scanner
2个回答
0
投票

缓冲区中有新行。多做一个nextLine来摆脱它。这样做吧。

String fullname = kb.nextLine();
kb.nextLine(); // gets rid of new line

您每次使用nextLine时都会这样做,因此下一个输入将被正确读取。并确保您确认返回的数组中有内容。


0
投票

如下替换getLast方法以避免NullPointerExceptionArrayIndexOutOfBoundsException

private static String getLast(String fullName) {
    String[] parts = fullName.split(" ");
    if(parts != null && parts.length == 2) {
        return parts[1];
    }
    return "";
}
© www.soinside.com 2019 - 2024. All rights reserved.