2015-03-19 118 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(); 
    } 

}