java中的数字格式异常,输错了

时间:2015-10-01 19:40:56

标签: java string class number-formatting

使用代码时

else if(command.equalsIgnoreCase("add")) {
     System.out.println("Enter the Student's Name: ");
        String name = input.nextLine();
     System.out.println("Enter the Student's Number: ");
        String studNum = input.nextLine();
     System.out.println("Enter the Student's Address: ");
        String address = input.nextLine();

     langara.addStudent(name, address, studNum);
     System.out.println("A Student added to the College Directory");
  }

如果用户输入add,则假设要经过上述过程,在拼贴类(langara)中有一个“addStudent”方法:

public void addStudent(String name, String address, String iD) {
  Student firstYear = new Student(name, address, iD);
  collegeStudents.add(firstYear);


}

这将使用构造函数创建学生类的学生对象:

public Student(String name, String address, String iD) {
  long actualId = Long.parseLong(iD);
  studentName = name;
  studentID = actualId;
  studentAddress = new Address(address);
  numberOfQuizzes = 0;
  scoreTotal = 0;


}

我收到了错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Abernathy, C."
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at Student.<init>(Student.java:48)
at College.addStudent(College.java:33)
at CollegeTester.main(CollegeTester.java:53)

就好像它正在尝试将学生姓名转换为长,但它应该将学生ID转换为长...

这是创建扫描仪,创建学院和初始化命令的地方:

Scanner input = new Scanner(System.in);

College langara = new College();

String command = "";

System.out.print("College Directory Commands:\n" +
                        "add - Add a new Student\n" +
                        "find - Find a Student\n" +
                        "addQuiz - Add a quiz score for a student\n" +
                        "findHighest - Find a student with the highest quiz score\n" +
                        "delete - Delete a Student\n" +
                        "quit - Quit\n");

command = input.nextLine();

正在从input.txt文件中读取输入,其中每个输入都在其自己的行上。 此命令的文件开头的输入是:

add
Abernathy, C.
10010123
100, West 49 Ave, Vancouver, BC, V7X2K6

这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

现在看来,但我发誓,当我第一次看到这个(并复制/粘贴你的代码)时,对langara.addStudent的调用的参数为name,studNum,address。我将这个类与您的输入文件放在一起,它似乎工作正常:



    import java.io.FileInputStream;
    import java.util.Scanner;

    public class StackOverflow_32895589 {

        public static class Student 
        {
            String studentName;
            long studentID;
            String studentAddress;
            long numberOfQuizzes;
            long scoreTotal;

            public Student(String name, String address, String iD) 
            {
                  studentName = name;
                  studentID = Long.parseLong(iD);
                  studentAddress = address;
                  numberOfQuizzes = 0;
                  scoreTotal = 0;
            }
        }

        public static void main(String[] args) 
        {
            try{
                System.setIn(new FileInputStream("c:\\temp\\input.txt"));           
            }
            catch (Exception e)
            {
                throw new RuntimeException(e);
            }
            Scanner input = new Scanner(System.in);
            String command = input.nextLine();

            if (command.equals("add"))
            {

             System.out.println("Enter the Student's Name:");
                String name = input.nextLine();
             System.out.println("Enter the Student's Number:");
                String studNum = input.nextLine();
             System.out.println("Enter the Student's Address:");
                String address = input.nextLine();
             System.out.println("[" + name + "][" + studNum + "][" + address + "]");
             input.close();
             addStudent(name, address, studNum);
            }
        }

        public static void addStudent(String name, String address, String iD) 
        {
                  Student firstYear = new Student(name, address, iD);
        }   
    }