Please help me. It goes like this:
Write a program that will determine the income of an employee. An employee is either a part-time or a full-time employee. A part time employee's gross income is computed as the product of his/her hourly rate and the number of hours worked. The gross income of a full time employee is computed as regular pay plus overtime pay. The overtime pay is computed as overtime rate multiplied by the number of overtime hours. The overtime pay should be computed if and only if the overtime hours rendered is not zero..

Dani AI

Generated

A simple, robust approach is to ask only for the inputs that apply to the chosen employee type and to compute and print the income after the chosen branch finishes. In the original post collected every field unconditionally, which is why part-time runs still prompt for overtime. was right to recommend guarding those prompts with an if/else — collect part-time inputs only when the choice is part-time, and collect overtime rate only when overtime hours are nonzero.

Example implementation that keeps prompting conditional and avoids Scanner/nextInt pitfalls:

import java.util.Scanner;

public class EmployeeIncome {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Name: ");
    String name = sc.nextLine();

    System.out.print("Enter 1 for Part-time, 2 for Full-time: ");
    int kind = Integer.parseInt(sc.nextLine().trim());

    double income = 0.0;
    if (kind == 1) {
      System.out.print("Hourly rate: ");
      double rate = Double.parseDouble(sc.nextLine().trim());
      System.out.print("Hours worked: ");
      double hours = Double.parseDouble(sc.nextLine().trim());
      income = rate * hours;
    } else if (kind == 2) {
      System.out.print("Regular pay: ");
      double regular = Double.parseDouble(sc.nextLine().trim());
      System.out.print("Overtime hours (0 if none): ");
      double otHours = Double.parseDouble(sc.nextLine().trim());
      if (otHours > 0) {
        System.out.print("Overtime rate: ");
        double otRate = Double.parseDouble(sc.nextLine().trim());
        income = regular + otRate * otHours;
      } else {
        income = regular;
      }
    } else {
      System.out.println("Invalid choice.");
    }

    System.out.printf("Name: %s%nIncome: %.2f%n", name, income);
    sc.close();
  }
}

Quick troubleshooting checklist:

  • Avoid mixing nextInt() and nextLine() without consuming the leftover newline; using nextLine() then parsing is simpler.
  • Print the computed income after assignment (the original code printed Income before it was set).
  • For real currency work, prefer BigDecimal to double to avoid rounding issues.
  • Replace any invalid initializations (for example an attempt to set char with a slash expression) with a single char or a boolean flag.
  • Add input validation loops to handle non-numeric input or invalid choices.

Recommended Answers

All 9 Replies

help you with what? what have you got so far?

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

import java.io.*;
import java.util.*;
import java.util.Scanner;

 public class income{
        static Scanner console=new Scanner(System.in);
          public static void main (String[]args)  { 

          String name = "";
          int kindOfEmployee;
          int pIncome = 0;
          int fIncome = 0;
          int overtimePay = 0;
          int HRate;
          int HWorked;
          int regularPay;
          int overtimeRate;
          int overtimeHours;
          int Income = 0;

          String comment = "";
          char registered = 'Y'/'N';

          Scanner input = new Scanner(System.in);

          System.out.println("Enter Name of the employee:");
          name = console.next ();
        System.out.println("1]Part-time Employee");
        System.out.println("2]Full-time Employee");
        System.out.println("Choose what kind of employee?");
        kindOfEmployee=console.nextInt();

        System.out.println("Hourly Rate:");
          HRate = input.nextInt();
          System.out.println("Hours Worked:");
          HWorked = input.nextInt();
          pIncome = HRate*HWorked; 

          System.out.println("Regular Pay:");
          regularPay = input.nextInt(); 
        System.out.println("Overtime Rate:");
          overtimeRate = input.nextInt();
          System.out.println("Overtime Hours:");    
          overtimeHours = input.nextInt();   
          overtimePay = overtimeRate*overtimeHours;
          fIncome = regularPay+overtimePay; 

          System.out.println("Name of the employee: "+name);
          System.out.println("Income: "+Income);

          if (kindOfEmployee == 1){
              Income = pIncome;          
                   }
          if (kindOfEmployee == 2){
               Income = fIncome;        
                }
        }
    }

there's something missing in the codes.. if i choose part time(no.1), it must only show hourly rate and hours worhed to compute for income.. but the overtime pay , overtime hours and overtime rate keeps on showing when they are supposed to come out if i choose (no.2)

i forgot to remove the entries in no.21 and 22... but i know it has nothing
to do with the problem..

what i'm referring to that bi'll choose from is the kind of employee in entry no.28 and no.29

what i'm referring to that i'll choose from is the kind of employee in entry no.28 and no.29

Use an if test to control when you print the overtime info...

// pseudo-code
if (employee is full time) {
   print overtime variables
}

thank you sir.. i got it !!! :))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.