2015-03-19 117 views
0

我想從文本文件中打印多個信息,如打印nameOfEmployee,hourlyRate,hoursWorked,taxRate,grossAmount,netAmount,但它只給我java.util.InputMismatchException,並且在控制檯中並非全部來自員工的信息僅打印姓名,小時收入,工作時間,稅收收入,我也想要合計員工的全部總額。從文本文件中掃描

private static Scanner ian; 

    public static void main (String[]args) throws FileNotFoundException 
    { 
     Scan(); 
    } 

    public static void Scan() 
    { 
     try 
     { 
      ian = new Scanner(new File("payroll.dat")); 
     } 
     catch (IOException e)  
     { 
      e.printStackTrace(); 
     } 

     while(ian.hasNext()) 
     { 
      String a = ian.nextLine(); 
      int b = ian.nextInt(); 
      int c = ian.nextInt(); 
      int d = ian.nextInt(); 
      int e = ian.nextInt(); 
      int f = ian.nextInt(); 

      System.out.printf("a= ", a , "b= ", b , " c= ", c , " d= ", d , "e = ", e , "f = " ,f); 
     } 
    } 
} 
+0

要獲得正確的理由例外不僅在掃描儀類的實例 – 2015-03-19 10:17:26

+0

幾乎你應該包內'try'和'catch'塊,你的代碼確定你不需要ian.nextLine(),nextInt應該自己換成新行。另外,你確定所有的值都是int,而不是小數? – 2015-03-19 10:19:41

+1

很明顯,文件內容和閱讀方式之間存在不匹配!但除非您向我們展示一些意見,否則我們無法告訴您原因。 – 2015-03-19 10:19:50

回答

0

請參閱 http://examples.javacodegeeks.com/core-java/lang/string/java-string-format-example/

編輯的代碼... 的printf的完整簽名的printf(字符串格式,對象...參數)。第一個參數是一個描述所需輸出格式的字符串。從那裏開始,printf可以有多個任意類型的參數。在運行時,這些參數將被轉換爲字符串,並將根據格式化指令進行打印。對於整數和%s
1%d的字符串:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Scanner; 




public class readfile { 
    private static Scanner ian; 

    public static void main (String[]args) throws FileNotFoundException 
    { 
     Scan(); 
    } 

    public static void Scan() 
    { 
     try 
     { 
      ian = new Scanner(new File("demo.txt")); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

     while(ian.hasNext()) 
     { 

      int b = ian.nextInt(); 
      int c = ian.nextInt(); 
      int d = ian.nextInt(); 
      int e = ian.nextInt(); 
      int f = ian.nextInt(); 
      String a = ian.nextLine(); 

      System.out.printf("a : %s, b : %d, c : %d ,d : %d ,e : %d, g : %d " ,a,b,c,d,e,f); 
     } 
    } 
} 
+0

a = :employeeName,這隻給我不是所有的細節 – unknown 2015-03-19 11:21:03

+0

現在檢查它..我編輯它..它工作.. :))) – 2015-03-19 11:24:17

+0

是的,它現在工作哈哈!非常感謝你:)) – unknown 2015-03-19 11:31:00

1

嘗試的東西更換的printf一行:

System.out.printf("a= %d b= %d c= %d d= %d e = %d f = %d" ,a,b,c,d,e,f); 

的printf就拿第一個參數字符串格式,然後其他的參數裏面

http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object...)

+1

是的,但這不是什麼導致異常。 – 2015-03-19 10:22:51

+0

我試過了,但這是唯一的輸出「a =」不是所有的值 – unknown 2015-03-19 10:46:53

0

如何格式化關於從java 7風格的文件中讀取?

public static void main(String[] arg) throws Exception { 
    try(Scanner scan = new Scanner(new File("payroll.dat"))){ 
     while(scan.hasNextLine()){ 
      // extract data here 
     } 
    } 
} 
+0

它給了我這個異常在線程「主」java.lang.NullPointerException \t在Try.main(Try.java:17) – unknown 2015-03-19 11:00:24

0

從文檔的Scannerhttp://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

nextLine():「此掃描器執行當前行,並返回跳過的輸入信息。」

您正在檢查是否有更多的輸入與hasNext(),但你打電話nextLine()這將跳過(並返回)整個下一行。然後你打電話給nextInt(),但是在你的最後一行之後,將不會有更多的整數。

你可能想是這樣的:

while(ian.hasNextLine()) 
    { 
     int b = ian.nextInt(); 
     int c = ian.nextInt(); 
     int d = ian.nextInt(); 
     int e = ian.nextInt(); 
     int f = ian.nextInt(); 
     String a = ian.nextLine(); 

     System.out.printf("a= ", a , "b= ", b , " c= ", c , " d= ", d , "e = ", e , "f = " ,f); 
    } 
} 

,但它取決於你的文件,你還沒有發佈的格式。

0

這是原創,我嘗試過,因爲我想知道如果它只是原來是錯的,我很抱歉沒有清除我的問題,而不是特別希望你能幫助傢伙。

private static final double FULL_TIME = 40.0; 

static Scanner scanner = new Scanner(System.in); 

static String nameOfEmployee; 
static double hourlyRate; 
static double hoursWorked; 
static double taxRate; 
static double grossAmount = 0; 
static double netAmount = 0; 

static double TOtalGrossAmount = 0; 
static double TotalNetAmount = 0; 

private static Scanner ian; 



public static void main (String []args) throws IOException 

    { 

     Instructions(); 

     PayrollReport(); 

     printEmployeeInfo(nameOfEmployee, hourlyRate, hoursWorked, taxRate, grossAmount, netAmount); 

    } 



public static void Instructions() 

{ 
    System.out.println("\t\t\t\t\t\tInstructions for Payroll Report Program" + 
      "\n\t\t\t\tThis program calculates a paycheck for each employee." + 
      "\n\t\t\t\tA text file containing the following information will be created." + 
      "\n\t\t\t\tname, pay rate, hours worked, and tax percentage to be deducted"); 

    System.out.println("\n\t\t\t\tThe program will create a report in columnar format showing the " + 
      "\n\t\t\t\ttemployee name, hourly rate, number of worked, tax rate," + 
      "\n\t\t\t\tgross pay, and netpay."); 

    System.out.println("\n\t\t\t\tAfter all emplyoees are processed , totals will be displayed," + 
      "\n\t\t\t\tincluding total gross amount and total net pay."); 
} 

public static void PayrollReport() 
{ 
    { 
     System.out.println("\n\t\t\t\t\t\t\t\tPayroll Report "); 
     System.out.print(""); 

     System.out.print("\n\t\t\t\tEmployee Name " + " Hourly Rate " + " Hours Worked " + " Tax Rate " + " Gross Amount " + " Net Amount"); 

     System.out.print(""); 
     System.out.println("\n\t\t\t\t--------------- " + " ----------- " + " ------------" + " --------- " + " ----------- " + " -----------");   
    }  
} 

public static void printEmployeeInfo(String nameOfEmployee , double HourlyRate , double HoursWorked , 
     double TaxRate , double GrossAmount , double NetAmount) throws IOException 
    { 

     try 
      { 

      ian = new Scanner(new File("payroll.dat")); 

      } 

     catch (IOException e)  

     { 
      e.printStackTrace(); 
     } 


    while (ian.hasNext())  
    { 
     nameOfEmployee = ian.nextLine(); 
     HourlyRate = ian.nextInt(); 
     HoursWorked = ian.nextInt(); 
     TaxRate = ian.nextInt(); 
     GrossAmount = 0; 
     NetAmount = 0; 

     TOtalGrossAmount += GrossAmount ; 
     TotalNetAmount += NetAmount; 

     System.out.printf("%s %s %s %s %s %s \n" , "\t\t\t\t" , nameOfEmployee , " " 
       , HourlyRate , " " , HoursWorked , " " , TaxRate , GrossAmount, NetAmount);  
    } 

    System.out.printf(" " , TOtalGrossAmount + " " + TotalNetAmount); 

     ian.close(); 
    } 

}