java - Ignoring that user needs to input a String -
i quite new @ programming, in first class, , have project due next week. now, know there lot of things can fixed in code, i'm trying figure out why won't allow me input ssn. programme when run far:
current month: 10
current week: 2
number of employees: 3
profile employee # 1
type of pay (hourly (h), salaried (s), salaried plus commission (c)): h
name: biff sanchez
social security number: birthday month (1-12):
i can't input social security number before asks me input birthday month. have tried nextline instead of next, doesn't alter anything.
my code below, please help. , give thanks you!
package payroll; import java.util.scanner; import java.text.*; public class payrollprogram { public static void main(string [] args) { decimalformat df = new decimalformat("###.00"); scanner kybd = new scanner(system.in); //get current month system.out.print("current month: "); int currentmonth = kybd.nextint(); if (currentmonth < 1 || currentmonth > 12) { system.out.print("error"); system.exit(0); } //get current week system.out.print("current week: "); int currentweek = kybd.nextint(); if (currentweek < 1 || currentweek > 4) { system.out.print("error"); system.exit(0); } //get number of employees system.out.print("number of employees: "); int numemployee = kybd.nextint(); int[] list = new int[numemployee]; //declare variables needed paycheck study //string type1 = ""; string [] type = new string[list.length]; string [] name = new string[list.length]; string [] ssn = new string[list.length]; double [] pay = new double[list.length]; double money = 0; //get info each employee for(int = 0; < list.length; i++) { system.out.print("profile employee # " + (i + 1) + "\n"); //get type of pay system.out.print("type of pay (hourly (h), salaried (s), salaried plus commision (c)): "); type[i] = kybd.next(); if (!type[i].equals("h") && !type[i].equals("s") && !type[i].equals("c")) { system.out.print("error"); system.exit(0); } //get name system.out.print("name: "); name[i] = kybd.next(); //get ssn system.out.print("social security number: "); ssn[i] = kybd.next(); //get birthday month system.out.print("birthday month (1-12): "); int bdaymonth = kybd.nextint(); if (bdaymonth < 1 || bdaymonth > 12) { system.out.print("error"); system.exit(0); } //get birthday bonus week system.out.print("birthday bonus week (1-4): "); int bdayweek = kybd.nextint(); if (bdayweek < 1 || bdayweek > 4) { system.out.print("error"); system.exit(0); } //get hourly pay (if hourly) if (type.equals("h")) { system.out.print("hourly pay:"); double hourlypay = kybd.nextdouble(); system.out.print("hours worked past week:"); double hours = kybd.nextdouble(); money = hourlypay * hours; } //get salary if salaried else if (type.equals("s")) { system.out.print("salary:"); double salary = kybd.nextdouble(); money = salary; } //get salary if salaried plus commission else if (type.equals("c")) { system.out.print("salary:"); double salary = kybd.nextdouble(); money = salary; system.out.print("sales past week: "); double salesweek = kybd.nextdouble(); system.out.print("sales commission rate (fraction paid employee): "); double commissionrate = kybd.nextdouble(); money = (commissionrate * salesweek) + money; } if (bdaymonth == currentmonth && bdayweek == currentweek) money = money + 100; system.out.print("\n"); pay[i] = money; kybd.close(); } system.out.print("\n"); /* */ // shows final results system.out.print("paycheck report:\n"); for(int = 0; < list.length; i++) { system.out.print("\nemployee:" + name[i]); system.out.print("\nsocial secuirty number:" + ssn[i]); system.out.print("\npaycheck:" + df.format(pay[i])); system.out.print(""); } } }
problem lies in input name , not in ssn.
change this
name[i] = kybd.next(); to
name[i] = kybd.nextline(); as input name can contain space need utilize nextline here.
java
No comments:
Post a Comment