0

These are my 3 classes. I am able to compile, but when I run it I can only type student 1 name and number. When I type student 2 number, an error occurs. enter image description here

Im really fuzzy about this and could use some help.

Thanks guys.

package lab4;

    public class Person {
    private String name;

        public Person(){

        }

        public void setName(String n){
            name = n;
        }

        public String getName(){
            return name;
        }

        public boolean hasSameName(Person s){
            return this.name.equals(s.getName());
        }

        public String toString(){
            return "Name: "+name;
        }
    }

    public class Student extends Person{
        private int studentNum;

        public Student(String n,int num){
            super();
            studentNum = num;
            setName(n);

        }

    //public void reset(String n,int number){
        //setName(n);

    //}

    public void setStudentNum(int n){
        studentNum = n;
    }

    public int getStudentNum(){
        return studentNum;
    }

    public boolean equals(Student s){
        return studentNum == s.getStudentNum();
    }

    public String toString(){
        return "Name:"+getName()+"\nStudent Number:"+studentNum;
    }
}

package lab4;
import java.util.Scanner;
public class Demo {
    public static void main(String [] args){

        Scanner kb = new Scanner(System.in);
        System.out.println("Enter first Student name and number:");
        Student s1 = new Student(kb.nextLine(),kb.nextInt());
        //System.out.println(s1);
        System.out.println("Enter second Student name and number:");
        Student s2 = new Student(kb.nextLine(),kb.nextInt());
        //System.out.println(s2);

        if(s1.equals(s2)&&s1.hasSameName(s2))
            System.out.println("Same student.");
        else
            System.out.println("Different person.");
    }
}
SaggingRufus
  • 1,769
  • 14
  • 31
Ratata4
  • 11
  • 1
  • This has **nothing** to do with inheritance and all to do with how Scanners work. I strongly urge you to read the answers in the [duplicate question](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) that was used to close this question. Then you will want to get the Scanner's `nextXxx(...)` methods out of parameters and on their own lines. Don't save space at the expense of increased risk of errors. – Hovercraft Full Of Eels Feb 11 '17 at 18:20
  • That works! tyty! – Ratata4 Feb 11 '17 at 18:54

0 Answers0