0

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);
    }
}
6
  • I think its due to the files contents...EMPTY scans fine (after Angela) and is pushed to setJobTitle. NextLine is nurse so nextDouble throws an input mismatch...just a guess Commented Aug 16, 2013 at 12:45
  • I want to show you that Angela, nurse and 2000.23 are in different lines, so I need to put the free line. It's connected with forum. Commented Aug 16, 2013 at 12:52
  • Why do you think the problem is related to netbeans? Commented Aug 16, 2013 at 12:57
  • I add this tag because I use this program. Commented Aug 16, 2013 at 13:02
  • Ok, comment out aScanner.nextLine(); (last line in loop) from the method and run the program Commented Aug 16, 2013 at 13:03

3 Answers 3

1

Your code is perfectly fine. I encountered the same problem with the Java for Dummies book.

For me, the problem lay within the formatting of the file. I am no expert (yet), so I am sorry to be unable to explain in further detail, but the InputMismatchException is thrown because I was using a . to separate the decimals while my system's standard decimals are separated by a ,

I propose you try formatting your file like this:

Angela
nurse
2000,15
Sign up to request clarification or add additional context in comments.

Comments

0

you can write like this also

static void payOneEmployee(Scanner aScanner)
    {
        Employee anEmployee = new Employee();

        List<String> employeeValueList = new ArrayList();
        while (aScanner.hasNext())
        {
            employeeValueList.add(aScanner.next());
        }

        if (!employeeValueList.isEmpty())
        {
            anEmployee.setName(employeeValueList.get(0));
            anEmployee.SetJobTitle(employeeValueList.get(1));
            anEmployee.cutCheck(new Double(employeeValueList.get(2)));
        }

    }

Comments

0

if - EmployeeInfo.txt


Angela

nurse

2000.23


in the Code
DoPayroll - class
payOneEmployee -functions line no 4

anEmployee.setName(aScanner.nextLine()); // this line will take input - Angela
anEmployee.SetJobTitle(aScanner.nextLine()); // this will take zero input because second line dont have any data
anEmployee.cutCheck(aScanner.nextDouble()); // this line take input as - nurse
//and "nurse" is not double so after reading this line at time of casting
from nurse to duble it throw java.util.InputMismatchException

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.