Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6181977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:07:22+00:00 2026-05-24T01:07:22+00:00

This program is supposed to run through a while loop twice. The first time

  • 0

This program is supposed to run through a while loop twice. The first time it runs through, it displays the name, occupation, age, salary and salary w/bonus. Yet, when the loop runs through for a second time, the name field is blank and the salary w/bonus is added onto the salary of the first employee. Thanks.

import java.util.Scanner;

public class Worker {

    public static void main(String[] args) {

        String name;
        int age;
        String occupation;
        double salary;
        double total = 10000;
        int count = 0;

        Employee employeeInfo = new Employee();

        Scanner keyboard = new Scanner(System.in);

        while (count < 2) {

            //User Name
            System.out.println("Enter your name: ");
            name = keyboard.nextLine();
            employeeInfo.setEmployeeName(name);

            keyboard.nextLine();

            //User occupation 
            System.out.println("Enter your occupation: ");
            occupation = keyboard.nextLine();
            employeeInfo.setOccupation(occupation);

            //User age
            System.out.println("Enter your age: ");
            age = keyboard.nextInt();
            employeeInfo.setAge(age);

            //User salary
            System.out.println("Enter your salary: ");
            salary = keyboard.nextDouble();
            employeeInfo.setSalary(salary);
            total = total + salary;

            //Output information
            System.out.println("Name: " + name);
            System.out.println("Age: " + age);
            System.out.println("Occupation: " + occupation);
            System.out.println("Original Salary: " + salary);
            System.out.println("Salary with bonus: " + total);

            count++;
        }

        // employeeInfo.greetingMessage("");

    }

}





public class Employee {

    private String employeeName;// Employee Name
    private int age;// Employee Age
    private String occupation;// Employee job title
    private double salary;// Employee salary

    // Setters
    public void setEmployeeName(String name) {

        employeeName = name;// Initializes employee name

    }

    public void setAge(int num) {
        age = num;// Initializes employee age
    }

    public void setOccupation(String occu) {

        occupation = occu;// Initializes occupation
    }

    public void setSalary(double num2) {

        salary = num2;// Initializes salary
    }

    // Getters

    public String getEmployeeName() {

        return employeeName;
    }

    public int getAge() {

        return age;
    }

    public String getOccupation() {

        return occupation;
    }

    public double getSalary() {

        return salary;
    }


    public void greetingMessage(String greeting){
        System.out.println("Greetings " + getEmployeeName());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:07:23+00:00Added an answer on May 24, 2026 at 1:07 am

    There is an issue with Scanner. You shouldn’t call nextLine after nextInt – I read that here. You need to make a second scanner and use it to capture the numbers, like so:

    Scanner keyboard = new Scanner(System.in);
    Scanner key2 = new Scanner(System.in);
    

    When you want a string use the first one and for a number use the second:

    name = keyboard.nextLine();
    age = key2.nextInt();
    

    Also, instantiate your Employee object inside the while loop or it will be overwritten with each iteration.

    while (count < 2) {
      employeeInfo = new Employee();
    

    Finally, remove the 2nd nextLine after you get the name but before you get the occupation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say suppose I am running a java program through command line. And this program
This program I use has it's own variables to set when you run it,
This is a program I'm writing that's supposed to display some text in a
Ok, so I have a simple c++ program that's supposed to run a couple
This program is supposed to calculate the number of degrees below 60 on a
This is for a script I'm working on. It's supposed to run an .exe
If I run this simple program on Windows 7 and then on AIX (Unix
Suppose that I have a Java program within an IDE (Eclipse in this case).
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.