coopwatts
coopwatts

Reputation: 690

Number format exception in java, taking wrong input

When using the code

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");
  }

If the user enters add, it's suppose to go through the above procedure, In the collage class (langara) there is a "addStudent" method :

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


}

And this creates a student object of the student class using the constructor:

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;


}

I'm getting the error:

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)

It's as if it's trying to convert the Students name to long, but it's supposed to convert the student ID to long..

This is where scanner is created, college is created, and command is initialized:

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

The input is being read from a input.txt file, with each input on it's own line. The input at the beginning of the file for this command is:

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

What is going wrong here?

Upvotes: 0

Views: 511

Answers (1)

Michael
Michael

Reputation: 539

It looks right now, but I would swear that when I first looked at this (and copy/pasted your code) that the call to langara.addStudent had the parameters as name, studNum, address. I put this class together with your input file and it appears to work fine:



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

Upvotes: 1

Related Questions