我的代码对我来说足够好,但我不断收到空指针异常。我正在输入一个如下所示的文本文件:
克米特 D.Frogge 17.25 5.25 0.0 8.0 1.5 8.0 2.25 7.75 0.0 8.0 2.0
这是我的一些代码:
private static BufferedReader br;
private static StringTokenizer strTkn;
private static String st, firstName, middleInitial, lastName;
private static double monRegHours, monOverHours, tuesRegHours, tuesOverHours, wedRegHours, wedOverHours,
thurRegHours, thurOverHours, friRegHours, friOverHours;
private static double totalRegHours, totalOverHours, hourlyWage, regularPay, overtimePay, weeklyPay;
private static JFileChooser chooser;
public static void main (String args []) throws IOException
{
chooser();
getData();
calcpay();
printresult();
}
public static void chooser() throws IOException
{
JFileChooser chooser=new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File f = chooser.getSelectedFile();
br=new BufferedReader(new FileReader(f));
st="";
while((st=br.readLine())!=null);
{
System.out.println ("Data Line = " + st ); //print the whole line
}
}
}
public static void getData() throws IOException
{
strTkn = new StringTokenizer(st, " .");
firstName = strTkn.nextToken();
middleInitial = strTkn.nextToken();
lastName = strTkn.nextToken();
hourlyWage = Double.parseDouble(strTkn.nextToken());
monRegHours = Double.parseDouble(strTkn.nextToken());
monOverHours = Double.parseDouble(strTkn.nextToken());
tuesRegHours = Double.parseDouble(strTkn.nextToken());
tuesOverHours = Double.parseDouble(strTkn.nextToken());
wedRegHours = Double.parseDouble(strTkn.nextToken());
wedOverHours = Double.parseDouble(strTkn.nextToken());
thurRegHours = Double.parseDouble(strTkn.nextToken());
thurOverHours = Double.parseDouble(strTkn.nextToken());
friRegHours = Double.parseDouble(strTkn.nextToken());
friOverHours = Double.parseDouble(strTkn.nextToken());
}
public static void calcpay()
{
totalRegHours = monRegHours + tuesRegHours + wedRegHours + thurRegHours + friRegHours;
totalOverHours = monOverHours + tuesOverHours + wedOverHours + thurOverHours + friOverHours;
overtimePay = round(totalOverHours * (1.5 * hourlyWage));
regularPay = round(hourlyWage * totalRegHours);
weeklyPay = regularPay + overtimePay;
}
public static void printresult()
{
System.out.println("Name: " + firstName + " " + middleInitial + " " + lastName);
System.out.printf("Weekly Pay: " + "$%.2f\n", weeklyPay);
//this allows me to print $x.50 instead of $x.5, and not have repeating decimals
}
public static double round(double num)
{
// rounding to two decimal places
num *= 100;
int rounded = (int) Math.round(num);
return rounded/100.0;
对不起,如果它看起来不好,我的电脑不喜欢复制粘贴