I'm learning from Java for Dummies and I don't know why I get these errors. I Googled some information.
java.util.InputMismatchException means that I want to read wrong type of values. For example file looks like:
2543
Robert
and I force the program to take from first line string. In my opinion everything in my file look right. I compared my code to sample code in the book and I can't find any mistakes.
I use Netbeans.
The File "EmployeeInfo" look like this:
Angela
nurse
2000.23
The main class:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class DoPayroll {
public static void main(String[] args) throws IOException{
Scanner diskScanner = new Scanner (new File("EmployeeInfo.txt"));
payOneEmployee(diskScanner);
}
static void payOneEmployee(Scanner aScanner)
{
Employee anEmployee = new Employee();
anEmployee.setName(aScanner.nextLine());
anEmployee.SetJobTitle(aScanner.nextLine());
anEmployee.cutCheck(aScanner.nextDouble());
aScanner.nextLine();
}
}
The class:
public class Employee {
private String name;
private String jobTitle;
public void setName(String mName)
{
name = mName;
}
public String GetName()
{
return name;
}
public void SetJobTitle(String mJobTitle)
{
jobTitle = mJobTitle;
}
public String GetJobTitle()
{
return jobTitle;
}
public void cutCheck(double amountPaid)
{
System.out.printf("Pay to the order of %s", name);
System.out.printf("%s ***€", jobTitle);
System.out.printf("%,.2f\n", amountPaid);
}
}
EMPTYscans fine (after Angela) and is pushed to setJobTitle. NextLine isnurseso nextDouble throws an input mismatch...just a guessaScanner.nextLine();(last line in loop) from the method and run the program