java - NumberFormatException: For input string: "8:00" -
so problem this? gives me error
exception in thread "main" java.lang.numberformatexception: input string: "8:00" @ java.lang.numberformatexception.forinputstring(unknown source) @ java.lang.integer.parseint(unknown source) @ java.lang.integer.parseint(unknown source) @ payroll.main(payroll.java:49)
here code:
import java.io.*; import java.text.*; public class payroll{ static bufferedreader in = new bufferedreader(new inputstreamreader(system.in)); public static void main(string [] args) throws ioexception { string empcode = ""; while(!empcode.equals('0')) { system.out.print("enter employee code: " ); empcode = in.readline(); string empinfo[] = getinfo(empcode); double basicsalary = double.parsedouble(empinfo[3]); system.out.println("read me first : ****************************"); system.out.println(" * input regular hours — > 8:00 – 17:00 "); system.out.println(" * input ot hours — > 17:30 – 20:30 "); system.out.println(" * ot income = ( basic salary / 8 ) * 1.1 "); system.out.println(" * holiday = ( basic salary / 8 ) * 1.1 "); system.out.println("*******************************************"); system.out.println("============= employee information =========="); system.out.println("employee code: " + empinfo[0]); system.out.println("employee name: " + empinfo[1]); system.out.println("employee position: " + empinfo[2]); system.out.println("employee basic salary: " + empinfo[3]); system.out.println("======================================="); string days[] = {"monday","tuesday","wednesday","thursday","friday"}; double timeinout[][] = new double[2][5]; double otinout[][] = new double[2][5]; string strtemp = ""; string tmptime[] = new string[2]; double tmphours, totalhours = 0, tmpregincome = 0, totalregincome = 0, tmpothours,totalothours = 0, tmpotincome, totalotincome = 0; for(int = 0; < 5; i++){ system.out.print("time in " + days[i] + ": "); strtemp = in.readline(); tmptime = strtemp.split(" : "); timeinout[0][i] = double.parsedouble(tmptime[0]) + (double.parsedouble(tmptime[1]) / 60); system.out.print("time out " + days[i] + " : "); strtemp = in.readline(); tmptime = strtemp.split(" : "); timeinout[1][i] = double.parsedouble(tmptime[0]) + (double.parsedouble(tmptime[1]) / 60); system.out.print("is " + days[i] + " holiday?: "); string isholiday = in.readline(); system.out.print("ot time in " + days[i] + " : "); strtemp = in.readline(); tmptime = strtemp.split(" : "); otinout[0][i] = double.parsedouble(tmptime[0]) + (double.parsedouble(tmptime[1]) / 60); system.out.print("ot time out " + days[i] + " : "); strtemp = in.readline(); tmptime = strtemp.split(" : "); otinout[1][i] = double.parsedouble(tmptime[0]) + (double.parsedouble(tmptime[1]) / 60); if(timeinout[0][i] < 8)timeinout[0][i] = 8; if(timeinout[1][i] > 17)timeinout[1][i] = 17; if(otinout[0][i] < 17.5 && otinout[0][i] != 0)otinout[0][i] = 17.5; if(otinout[1][i] > 20.5)otinout[1][i] = 20.5; tmphours = timeinout[1][i] - timeinout[0][i]; tmpothours = otinout[1][i] - otinout[0][i]; if(tmphours > 4)tmphours--; if(isholiday.equals("yes")){ totalothours += tmphours; totalotincome += tmphours * ((basicsalary / 8) * 1.1); totalhours += tmphours; tmpregincome = tmphours * (basicsalary / 8); }else{ totalhours += tmphours; tmpregincome = tmphours * (basicsalary / 8); } totalothours += tmpothours; totalotincome += tmpothours * ((basicsalary / 8) * 1.1); totalregincome += tmpregincome; } double grossincome = totalregincome + totalotincome; decimalformat df = new decimalformat("#.##"); system.out.println("=========== total output =============="); system.out.println("total work hours: ” + df.format(totalhours)"); system.out.println("total regular income: ” + df.format(totalregincome)"); system.out.println("total ot hours: ” + df.format(totalothours)"); system.out.println("total ot income: ” + df.format(totalotincome)"); system.out.println("gross income: ” + df.format(grossincome)"); system.out.println("====================================="); } } static string[] getinfo(string empcode){ string getinfo[] = new string [4]; string strline; int ctr =0; boolean isfound = false; try{ fileinputstream fstream = new fileinputstream("payroll.txt"); datainputstream dstream = new datainputstream(fstream); bufferedreader br = new bufferedreader(new inputstreamreader(dstream)); while((strline = br.readline()) != null && ctr < 4){ if(strline.equals(empcode))isfound = true; if(isfound){ getinfo[ctr] = strline; ctr++; } } br.close(); }catch(ioexception e){ system.out.println("error: " + e.getmessage()); } return getinfo; } }
you're splitting string on " : "
, time string 8:00
, split should ":"
remove blank space. instead of tmptime = strtemp.split(" : ");
, try as: tmptime = strtemp.split(":");