0

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?

8
  • 4
    What the order of lines on the text file ? its should be name, ID, address because you read it on this order! Commented Oct 1, 2015 at 19:45
  • Are you sure that the text file is in the right order? Commented Oct 1, 2015 at 19:48
  • @WajdyEssam Yeah it's in the right order, I updated the posting with an example of how it's formatted in the input.txt file Commented Oct 1, 2015 at 19:59
  • 1
    We need to know what command is and where it comes from as well as what input is. It looks as though add is being read in as the name, not the command. Commented Oct 1, 2015 at 20:04
  • 1
    Try printing the value of name and tell us what gets printed. Commented Oct 1, 2015 at 20:23

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.