简体   繁体   English

从文本文件扫描

[英]Scan from text file

I am trying to print a multiple information from a text file like print the nameOfEmployee, hourlyRate, hoursWorked, taxRate, grossAmount, netAmount but it only gives me java.util.InputMismatchException and in the console not all of the information from a employee is printed only the name, hourlyRate, hoursWorked, taxRate, also i want to Total all the grossAmount of my employees. 我正在尝试从文本文件中打印多个信息,例如打印nameOfEmployee,hourlyRate,hoursWorked,taxRate,grossAmount,netAmount,但它只给我java.util.InputMismatchException,并且在控制台中,并非打印了来自雇员的所有信息我只想要名称,hourlyRate,hoursWorked,taxRate,我也想总计所有我的员工的grossAmount。

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

Try replacing the printf line by something like : 尝试用类似以下内容替换printf行:

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

printf take as first argument the String format, and then other argument to be formatted inside printf将String格式作为第一个参数,然后在内部将其格式化的其他参数

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

How about reading from file in java 7 style? 如何以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
        }
    }
}

From the docs for Scanner http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html Scanner的文档中http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

nextLine() : "Advances this scanner past the current line and returns the input that was skipped." nextLine() :“使该扫描仪前进到当前行之外,并返回被跳过的输入。”

You're checking if there is more input with hasNext() , but then you call nextLine() which will skip (and return) the entire next line. 您正在使用hasNext()检查是否有更多输入,但是然后调用nextLine() ,它将跳过(并返回)整个下一行。 Then you call nextInt() , but after your last line, there won't be any more integers. 然后,您调用nextInt() ,但是在最后一行之后,将不再有整数。

You probably want something like this: 您可能想要这样的东西:

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

but it depends on the format of your file, which you haven't posted. 但这取决于您尚未发布的文件格式。

this is the original , i tried that because i want to know if it's just the original is wrong , I'm sorry for not clearing my question and not being specifically hope you can help guys . 这是原始的,我尝试过,因为我想知道它是否只是原始的是错误的,对不起您未清除我的问题,也没有特别希望您能为大家提供帮助感到抱歉。

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

} }

please do refer http://examples.javacodegeeks.com/core-java/lang/string/java-string-format-example/ 请确实参考http://examples.javacodegeeks.com/core-java/lang/string/java-string-format-example/

Edited code... The complete signature of printf is printf(String format, Object... args). 编辑的代码... printf的完整签名是printf(字符串格式,对象... args)。 The first argument is a String that describes the desired formatting of the output. 第一个参数是一个字符串,它描述所需的输出格式。 From there on, printf can have multiple number of arguments of any type. 从那里开始,printf可以具有多个任意类型的参数。 At runtime, these arguments will be converted to String and will be printed according to the formatting instructions. 在运行时,这些参数将转换为String并根据格式说明进行打印。
1.%d for integers and %s for String: 1.%d代表整数,%s代表字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM