java I / O&例外

时间:2014-04-28 04:17:35

标签: java object java.util.scanner

我在尝试让我的代码读取.dat文件并使用其内容填写信息时遇到了一些麻烦。我不太确定下面的代码是否是正确的方法。

代码不会运行,但会编译吗?我正在使用Java博士的macbook。

更新:所以这不是代码,而是软件...只是下载了另一个java程序,谢谢大家的帮助,我不会自己想出来的!

import java.io.File;
import java.io.*;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Pay {
    public static void main (String [] args) throws Exception{
    File file = new File("c:\\rate.dat");  
      Scanner input = new Scanner(file); 
  // I was not too sure if the last two lines are the correct ways to call the .dat file

      int id;
      String last;
      String first;
      double h![enter image description here][1]oursWorked;
      double rate;
      double total;

   for (int count = 1; count <= 4; count++) { //the file only needs to be ran 4 times 
         System.out.println("Employee I.D.: ");
         id = input.nextInt();

         System.out.println("Last Name: ");
         last = input.next();

         System.out.println("First Name: ");
         first = input.next();

         System.out.println("Hours Worked: ");
         hoursWorked = input.nextDouble();

         System.out.println("Hourly Rate: ");
         rate = input.nextDouble();   

         total = totalPay (hoursWorked, rate);

         System.out.println("\nEmployee ID  :" + id);
         System.out.println("\nLast Name    :" + last);
         System.out.println("\nFirst Name   :" + first);
         System.out.println("\nHours Worked :" + hoursWorked);
         System.out.println("\nPay Rate     :" + rate);
         System.out.println("\nTotal Pay    :" + total);
     }
  }
 //Total Pay
 public static double totalPay (double time, double r) {
        double total = 0.0;
         if (time <= 40.0) {
          total = (time * r);

         }
          else if (time > 40.0) {
            total = (((time - 40) * (r * 1.5)) + (40 * r));            
          }
     return total;
  }
 }   

rate.dat文件包含:

1
Washington
Geroge
45
30
210
Jackson
Andrew
55
20.5
299
Obama
Barak
10
20
405
Reagan
Ronald
59
40

3 个答案:

答案 0 :(得分:3)

您的代码中有一点错误

double HoursWorked;

应该是

double houseWorked;

答案 1 :(得分:0)

Pay.java不可编译,因为声明了

(Error:(29, 14) java: cannot find symbol
symbol:   variable hoursWorked
location: class Pay)

hoursWorked = input.nextDouble();

我添加了声明

double hoursWorked = input.nextDouble();

它在控制台上使用以下输出:

Pay Rate     :20.5

Total Pay    :1281.25
Employee I.D.:
Last Name:
First Name:
Hours Worked:
Hourly Rate:

Employee ID  :299

Last Name    :Obama

First Name   :Barak

Hours Worked :10.0

Pay Rate     :20.0

Total Pay    :200.0
Employee I.D.:
Last Name:
First Name:
Hours Worked:
Hourly Rate:

Employee ID  :405

Last Name    :Reagan

First Name   :Ronald

Hours Worked :59.0

Pay Rate     :40.0

Total Pay    :2740.0

答案 2 :(得分:-2)

import java.io.File;
import java.io.*;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Pay {
    public static void main (String [] args) throws Exception{
    File file = new File("c:\\rate.dat");  
      Scanner input = new Scanner(file); 
  // I was not too sure if the last two lines are the correct ways to call the .dat file

      int id;
      String last;
      String first;
      double HoursWorked;
      double rate;
      double total;

   for (int count = 1; count <= 4; count++) { //the file only needs to be ran 4 times 
         id = input.nextInt();
         last = input.next();
         first = input.next();
         HoursWorked = input.nextDouble();
         rate = input.nextDouble();   
         total = totalPay (HoursWorked, rate);

         System.out.println("\nEmployee ID  :" + id);
         System.out.println("\nLast Name    :" + last);
         System.out.println("\nFirst Name   :" + first);
         System.out.println("\nHours Worked :" + HoursWorked);
         System.out.println("\nPay Rate     :" + rate);
         System.out.println("\nTotal Pay    :" + total);
     }
  }
 //Total Pay
 public static double totalPay (double time, double r) {
        double total = 0.0;
         if (time <= 40.0) {
          total = (time * r);

         }
          else if (time > 40.0) {
            total = (((time - 40) * (r * 1.5)) + (40 * r));            
          }
     return total;
  }
 }