2015-03-31 98 views
0

我想從一個文本文件中讀取數據,並使用該文件中的一些文本來計算他們的加班工資,並將其輸出到一個稱爲加班的文件。文本。讀取,計算和寫入數據到/從.txt文件

數據文件應該是這樣的:

bob 
50 
2.3 
julio 
60 
60.00 

到目前爲止我的代碼低於:

import javax.swing.JOptionPane; 

import java.io.*; 
import java.text.DecimalFormat; 

public class ReadData { 
public ReadData() 
{ 
    try { 
     String[] firstLine = new String[100], 
     secondLine = new String[100], 
     thirdLine = new String[100]; 

     double hours[] = new double[100], wages[] = new double[100]; 
     String result[] = new String[100], formattedGrossPay[] = new String[100]; 
     double grossPay[] = new double[100]; 
     double excessHours[] = new double[100], extraPay[] = new double[100]; 
     String stringExcessHours[] = new String[100]; 

     int index; 

     for (index = 0; index < 100; index++) { 
     firstLine[index] = ""; 
     secondLine[index] = ""; 
     thirdLine[index ] = ""; 
     hours[index] = 0.0; 
     wages[index]= 0.0; 
    } 
    FileReader file = new FileReader("payroll.txt"); 
    BufferedReader buffer = new BufferedReader(file); 
    index = 0; 
    String line; 

    File check = new File("overtime.txt"); 
    FileWriter file2; 
    if(check.exists()) 
     file2 = new FileWriter("overtime.txt", true); 
    else 
     file2 = new FileWriter("overtime.txt"); 
    BufferedWriter buffer2 = new BufferedWriter(file2); 

    /* 
    FileWriter file1 = new FileWriter("overtime.txt"); 
    BufferedWriter buffer1 = new BufferedWriter(file1); 
    */ 

    while((line = buffer.readLine()) != null) 
    { 
     firstLine[index] = line; 
     secondLine[index] = buffer.readLine(); 
     thirdLine[index ] = buffer.readLine(); 

     hours[index] = Double.parseDouble(secondLine[index]); 
     wages[index] = Double.parseDouble(thirdLine[index]); 
     DecimalFormat df = new DecimalFormat("#.00"); 
     result[index] = df.format(wages[index]); 
     grossPay[index] = (hours[index] * wages[index]); 
     if (hours[index] > 40) 
     { 
      excessHours[index] = hours[index]-40; 
      extraPay[index] = excessHours[index] * (.5 * wages[index]); 
      grossPay[index]=grossPay[index]+extraPay[index]; 
     } 
     else 
     { 
      excessHours[index]=0; 
     } 

     //grossPayString[index] = Double.toString(grossPay[index]); 
     formattedGrossPay[index] = df.format(grossPay[index]); 

     JOptionPane.showMessageDialog(null, "Name: " + firstLine[index] + "\nHours: " 
     + hours[index] + "\nWages: $" + result[index] + "\nGross Pay: $" + formattedGrossPay[index], "Result", 
     JOptionPane.PLAIN_MESSAGE); 
     index++; 

     stringExcessHours[index] = Double.toString(excessHours[index]); 
     buffer2.write(firstLine[index]); 
     buffer2.newLine(); 
     buffer2.write(stringExcessHours[index]); 
     buffer2.newLine(); 
    } 

    buffer.close(); 
    buffer2.close(); 
    System.exit(0); 

    }//end of try 
    catch (IOException e) { System.out.println(e); } 
    }//end of ReadData 

public static void main(String[] args) 
{ 
    new ReadData(); 
} 
} 

眼下,輸出到我的overtime.txt文件應該是這樣的:

0.0 

0.0 

我每次都得到0.0加班費,而不是在第一行打印名字,空白裏ne被打印。

任何幫助解決這個問題的正確寫入數據到overtime.txt文件將不勝感激。

+0

MessageDialog顯示正確的數字? – 2015-03-31 00:29:36

+0

是的。以字符串形式顯示名稱。工時和工資總額顯示爲雙打。 – pHorseSpec 2015-03-31 00:34:13

+0

我認爲@ScaryWombat解決方案將正確解決您的問題。 – 2015-03-31 00:35:02

回答

1

在你的代碼你遞增索引然後讀取陣列

 index++; 

    stringExcessHours[index] = Double.toString(excessHours[index]); 
    buffer2.write(firstLine[index]); 
    buffer2.newLine(); 
    buffer2.write(stringExcessHours[index]); 
    buffer2.newLine(); 

變化

 stringExcessHours[index] = Double.toString(excessHours[index]); 
    buffer2.write(firstLine[index]); 
    buffer2.newLine(); 
    buffer2.write(stringExcessHours[index]); 
    buffer2.newLine(); 

    index++; 
1

這工作:

import javax.swing.JOptionPane; 
import java.io.*; 
import java.text.DecimalFormat; 

public class ReadData { 
    public ReadData() 
    { 
     try { 
      String[] firstLine = new String[100], 
      secondLine = new String[100], 
      thirdLine = new String[100]; 

      double hours[] = new double[100], wages[] = new double[100]; 
      String result[] = new String[100], formattedGrossPay[] = new String[100]; 
      double grossPay[] = new double[100]; 
      double excessHours[] = new double[100], extraPay[] = new double[100]; 
      String stringExcessHours[] = new String[100]; 

      int index; 

      for (index = 0; index < 100; index++) { 
       firstLine[index] = ""; 
       secondLine[index] = ""; 
       thirdLine[index ] = ""; 
       hours[index] = 0.0; 
       wages[index]= 0.0; 
      } 
      FileReader file = new FileReader("payroll.txt"); 
      BufferedReader buffer = new BufferedReader(file); 
      index = 0; 
      String line; 

      File check = new File("overtime.txt"); 
      FileWriter file2; 
      if(check.exists()) 
       file2 = new FileWriter("overtime.txt", true); 
      else 
       file2 = new FileWriter("overtime.txt"); 

      BufferedWriter buffer2 = new BufferedWriter(file2); 

     /* 
      FileWriter file1 = new FileWriter("overtime.txt"); 
      BufferedWriter buffer1 = new BufferedWriter(file1); 
      */ 

      while((line = buffer.readLine()) != null) 
      { 
       firstLine[index] = line; 
       String name = firstLine[index]; 

       secondLine[index] = buffer.readLine(); 
       int hoursWorked = Integer.parseInt(secondLine[index]); 

       thirdLine[index ] = buffer.readLine(); 
       double wage = Double.parseDouble(thirdLine[index]); 

       hours[index] = hoursWorked; 
       wages[index] = wage; 
       DecimalFormat df = new DecimalFormat("#.00"); 
       result[index] = df.format(wages[index]); 
       grossPay[index] = (hours[index] * wages[index]); 

       if (hours[index] > 40) 
       { 
        excessHours[index] = hours[index]-40; 
        extraPay[index] = excessHours[index] * (.5 * wages[index]); 
        grossPay[index]=grossPay[index]+extraPay[index]; 
       } 
       else 
       { 
        excessHours[index]=0; 
       } 

       //grossPayString[index] = Double.toString(grossPay[index]); 
       formattedGrossPay[index] = df.format(grossPay[index]); 

       JOptionPane.showMessageDialog(null, "Name: " + firstLine[index] + "\nHours: " 
        + hours[index] + "\nWages: $" + result[index] + "\nGross Pay: $" + formattedGrossPay[index], "Result", 
       JOptionPane.PLAIN_MESSAGE); 


       stringExcessHours[index] = Double.toString(excessHours[index]); 

       buffer2.write(name); 
       buffer2.newLine(); 
       buffer2.write("Excess Hours: " + stringExcessHours[index] + "\tExtra pay: " + extraPay[index]); 
       buffer2.newLine(); 
       index++; 
      } 

      buffer.close(); 
      buffer2.close(); 
      System.exit(0); 

     }//end of try 
     catch (IOException e) { System.out.println(e); } 
    }//end of ReadData 

    public static void main(String[] args) 
    { 
     new ReadData(); 
    } 
} 

請給我是+1,如果你像我的解決方案。

P.S. - 您可能更願意使用列表而非數組,因此大小可以是無限的(在此示例中,超過100名員工將導致ArrayIndexOutOfBounds異常)。您可能還想使用哈希映射而不是列表,將員工作爲關鍵字並將他們的工資作爲值。然後,您可以將員工的另一個散列圖作爲關鍵字,並將他們每週的工作時間作爲值。