netbeans 7 - java.util.InputMismatchException errors in reading from file, what's wrong with type? -
i'm learning java dummies , don't know why these errors. googled information.
java.util.inputmismatchexception means want read wrong type of values. example file looks like:
2543 robert and force program take first line string. in opinion in file right. compared code sample code in book , can't find mistakes.
i use netbeans.
the file "employeeinfo" 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 order of %s", name); system.out.printf("%s ***€", jobtitle); system.out.printf("%,.2f\n", amountpaid); } }
you can write
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
Post a Comment