在向用户询问电子邮件地址时,Java Scanner util抛出异常

时间:2017-02-02 23:36:48

标签: java java.util.scanner

我必须为课堂写一个名册。

作业要求您将自己的信息输入程序。

为此,我在我的测试类中添加了一个扫描器部分到一个新的Student对象(我),在该对象中它询问我所有的构造函数参数。

其中一个是电子邮件地址。我无法让它发挥作用,因为每次我输入我的电子邮件地址时都会遇到一系列例外情况。

以下是输出示例:

What is your first name?
Banning
What is your last name?
Lyth
What is your email address?
banninglyth@live.com
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at StudentRosterBLYTH.StudentRosterTest.main(StudentRosterTest.java:68)
C:\Users\banni\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 14 seconds)

这是我的测试类。最后一部分是抛出错误。

import java.util.ArrayList;
import java.util.Scanner;

/**
*
* @author banni
*/
public class StudentRosterTest {
    static int studentID;
    static String firstName
    static String lastName;
    static String emailAddress;
    static int age;
    static int[] grades;
    public static Scanner input = new Scanner(System. in );

    public static void main(String[] args) {

        ArrayList <Student> students = new ArrayList <Student> ();

        students.add(new Student(studentID, firstName, lastName, emailAddress, age));
        studentID = 1;
        firstName = "John";
        lastName = "Smith";
        emailAddress = "John1989@google.com";
        age = 20;

        students.add(new Student(studentID, firstName, lastName, emailAddress, age));
        studentID = 2;
        firstName = "Suzan";
        lastName = "Erickson";
        emailAddress = "Erickson_1990@gmail.com";
        age = 19;

        students.add(new Student(studentID, firstName, lastName, emailAddress, age));
        studentID = 3;
        firstName = "Jack";
        lastName = "Napol";
        emailAddress = "The_Lawyer99yahoo.com";
        age = 19;

        students.add(new Student(studentID, firstName, lastName, emailAddress, age));
        studentID = 4;
        firstName = "Erin";
        lastName = "Black";
        emailAddress = "Erin.black@comcast.net";
        age = 22;

        students.add(new Student(studentID, firstName, lastName, emailAddress, age));
        studentID = 5;
        //Begin asking for new Student entry
        firstName = input.next();
        System.out.println("What is your first name?");
        lastName = input.next();
        System.out.println("What is your last name?");
        emailAddress = input.next();
        System.out.println("What is your email address?");
        age = input.nextInt();
        System.out.println("How old are you?");

    }
}

所以,我向你们所有人提出的问题是,我做错了什么?它是At-Sign还是我完全关闭?

另外,给出了什么?我刚刚发布了这一分钟,已经有了-rep? wat.png

1 个答案:

答案 0 :(得分:1)

当您要求发送电子邮件时,您正在使用nextInt(),您的下一个电话将全部关闭。你应该使用.nextLine()表示String输入,使用.nextInt()表示年龄。

studentID = 5;
//Begin asking for new Student entry
System.out.println("What is your first name?");
firstName = input.nextLine();
System.out.println("What is your last name?");
lastName = input.nextLine();
System.out.println("What is your email address?");
emailAddress = input.nextLine();
System.out.println("How old are you?");
age = input.nextInt();