从文本文件中扫描

时间:2015-03-19 10:14:15

标签: java

我正在尝试从文本文件中打印多个信息,例如打印nameOfEmployee,hourlyRate,hoursWorked,taxRate,grossAmount,netAmount,但它只给了我java.util.InputMismatchException,并且在控制台中没有来自a的所有信息员工只打印名称,hourlyRate,hoursWorked,taxRate,我也想要总计我员工的总数。

    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);
        }
    }
}

5 个答案:

答案 0 :(得分:1)

尝试用以下内容替换printf行:

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

printf将String格式作为第一个参数,然后在

中格式化其他参数

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

答案 1 :(得分: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
        }
    }
}

答案 2 :(得分:0)

来自Scanner http://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);
    }
}

但这取决于您尚未发布的文件格式。

答案 3 :(得分: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();
    }

}

答案 4 :(得分:0)

请参考 http://examples.javacodegeeks.com/core-java/lang/string/java-string-format-example/

编辑代码...... printf的完整签名是printf(String format,Object ... args)。第一个参数是一个String,它描述了所需的输出格式。从那以后,printf可以有多个任何类型的参数。在运行时,这些参数将转换为String,并将根据格式说明进行打印 1.%d表示整数,%s表示String:

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);
        }
    }
}