0

The program runs everything up until the givePhoneNum() method for Person2 then terminates after printing the first line of code within the givePhoneNum method.

here is main:

import PersonInfo.Person;
import java.util.Scanner;
public class PersonStuff {

static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
    Person  person1=new Person();
    Person  person2=new Person(18,'f');

    person1.name="Bob";
    person1.age=21;
    person1.enterGender();

    person2.name="Jane";
    person2.givePhoneNum();
    person2.printPhoneNum();

}

}

here is the Person class:

package PersonInfo;

import java.util.Scanner;

public class Person {
static Scanner in=new Scanner(System.in);

public String name;
public int age;
private char gender;
private String phoneNum;

public Person(){ //Constructor 1

}

public Person(int agePerson, char genderPerson){ //Constructor 2
    this.age=agePerson;
    this.gender=genderPerson;
}

public Person(String personName, int personAge){ //Constructor 3
    this.name=personName;
    this.age=personAge;
}

// Methods

public void givePhoneNum(){
    System.out.print("Enter phone number for "+name+":");
    String phoneNum=in.nextLine();
    this.phoneNum = phoneNum;

}

public boolean checkGender(char g){
    if (g=='m' || g=='f'){
        return true;

    }
    else return false;
}

public void enterGender(){
    System.out.println("Enter gender:");
    char gender=in.next().charAt(0);
    //System.out.println(gender);
    this.gender=gender;
    checkGender(gender);
    if(checkGender(gender)==false){
        enterGender();
    }   

}

public void printPhoneNum(){
    System.out.println(phoneNum);
}

}

Any help would be very much appreciated. Thanks!

Chez
  • 23
  • 3
  • Please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Hovercraft Full Of Eels Apr 06 '17 at 02:20
  • Just check if in has next line by in.hasnextline() before in.nextline() – Fady Saad Apr 06 '17 at 02:21
  • Change `char gender=in.next().charAt(0);` to `char gender=in.nextLine().charAt(0);` as per the link that I mentioned above – Hovercraft Full Of Eels Apr 06 '17 at 02:21
  • You need to create one more instance like the following:public void givePhoneNum(){ System.out.print("Enter phone number for "+name+":"); in=new Scanner(System.in);//it should work String phoneNum=in.nextLine(); this.phoneNum = phoneNum; System.out.println("phno"+phoneNum); } – Shaik Elias Apr 06 '17 at 02:42

0 Answers0