Reputation: 397
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.
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);
}
}
}
Upvotes: 0
Views: 122
Reputation: 129
please do refer 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). 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. At runtime, these arguments will be converted to String and will be printed according to the formatting instructions.
1.%d for integers and %s for 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);
}
}
}
Upvotes: 0
Reputation: 397
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();
}
}
Upvotes: 0
Reputation: 8774
From the docs for 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."
You're checking if there is more input with hasNext()
, but then you call nextLine()
which will skip (and return) the entire next line. Then you call nextInt()
, but after your last line, there won't be any more integers.
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.
Upvotes: 0
Reputation: 219
How about reading from file in java 7 style?
public static void main(String[] arg) throws Exception {
try(Scanner scan = new Scanner(new File("payroll.dat"))){
while(scan.hasNextLine()){
// extract data here
}
}
}
Upvotes: 0
Reputation: 1205
Try replacing the printf line by something like :
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
Upvotes: 1