1

I have created two files one with having private variables and getters and setters, and other with taking input from the user and displaying the output in the console.

When I execute the program, it runs without error but when the input is given, it takes 3 inputs out of 4.

I am unable to get input for the fourth variable.

File with getters and setters

    package tryProject;

public class Employee {
    

     private String name;
     private int yearJoin;
     private  int salary;
     private  String address;
     
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getYearJoin() {
        return yearJoin;
    }
    public void setYearJoin(int yearJoin) {
        this.yearJoin = yearJoin;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }   
    
}

File to take input and give output

package tryProject;

import java.util.Scanner;

public class EmployeeInfo {

    public static void main(String[] args) {
        Employee e = new Employee();
    
        Scanner s = new Scanner(System.in);  
            
        System.out.println("Enter details: ");
        System.out.println("Name: ");
        String input_name = s.nextLine();
        e.setName(input_name);
        System.out.println(e.getName());
        
        System.out.println("Salary: ");
        int input_salary = s.nextInt();
        e.setSalary(input_salary);
        System.out.println(e.getSalary());
        
        System.out.println("Year of Join: ");
        int input_YearJoin = s.nextInt();
        e.setYearJoin(input_YearJoin);
        System.out.println(e.getYearJoin());
        
        System.out.println("Address: ");
        String input_Address = s.nextLine();
        e.setAddress(input_Address);
        System.out.println(e.getAddress());
        
    }

}

2 Answers2

0

I've tested your program and indeed it prints name, salary and year while it prints an empty line for address. The reason for that is when you give input for "year of join", you type some number and then press "Enter". When you press "Enter" you actually give an input of empty line ("\n") and that's still an input and it's taken for address field. That's why the program doesn't wait for your address line. If you use a debugger you'll notice that. If you change String input_Address = s.nextLine(); to String input_Address = s.next(); and then run your program you'll understand what I mean. However, I don't suggest this as a fix.

I suggest that you add scanner.nextLine(); after reading year of join. This way the program will read the next empty line which was generated by pressing "Enter" key. And then it will read your actual address data.

System.out.println("Year of Join: ");
int input_YearJoin = scanner.nextInt();
e.setYearJoin(input_YearJoin);
System.out.println(e.getYearJoin());
scanner.nextLine();

You can read this post for more insight: Scanner is skipping nextLine() after using next() or nextFoo()?

Juvanis
  • 25,000
  • 3
  • 61
  • 84
-1

Try .next() instead of .nextLine().

I would change it for the name too.

I changed your code so it works:

public static void main(String[] args) {
        Employee e = new Employee();
        Scanner s = new Scanner(System.in); 
            
        System.out.println("Enter details: ");
        System.out.println("Name: ");
        String input_name = s.next(); //changed .nextLine() to .next()
        e.setName(input_name);
        System.out.println(e.getName());
        
        System.out.println("Salary: ");
        int input_salary = s.nextInt();
        e.setSalary(input_salary);
        System.out.println(e.getSalary());
        
        System.out.println("Year of Join: ");
        int input_YearJoin = s.nextInt();
        e.setYearJoin(input_YearJoin);
        System.out.println(e.getYearJoin());
        
        System.out.println("Address: ");
        String input_Address = s.next(); //changed .nextLine() to .next()
        e.setAddress(input_Address);
    }

Hope this helped :)

Flori
  • 86
  • 7